6
\$\begingroup\$

Black Box is a board game, Your task is to reveal all clues.

What is black box

Black box is a board game with hidden atoms, Your task is given input, All atoms, reveal all clues.

I/O

Input

The atoms can be any 1-char that is not newline (Used for separator) Like this.

O....
...O.
..O..
.....
....O

Output

When the code reveals all clues (See Below) the output of The given input above can be:

 HRHH1
H     1
R     H
H     H
2     R
H     H
 H2HRH

or

[["H", "R", "H", "H", "1"], ["1", "H", "H", "R", "H"], ["H", "R", "H", "2", "H"], ["H", "2", "H", "R", "H"]]

Ignore the number, This doesn't mean anything, the number must be non-unique and must work with >9 detours

Task

Hit

Atoms interact with rays in three ways. A direct impact on an atom by a ray is a "hit".

.........
.........
.O.....O.
.........
.........
...O.....H 1
.........
.......O.

Thus, ray 1 fired into the box configuration at left strikes an atom directly, generating a "hit", designated by an "H". A ray which hits an atom does not emerge from the box.

Deflection

The interaction resulting from a ray which does not actually hit an atom, but which passes directly to one side of the ball is called a "deflection". The angle of deflection for this ray/atom interaction is 90 degrees. Ray 2 is deflected by the atom at left, exiting the box as shown.

    2
.........
.........
.O.....O.
.........
.........2
...O.....
.........
.......O.

Explanation in ASCII art:

    2
....^....
....^....
.O..^..O.
....^....
....\<<<<2
...O.....
.........
.......O.

Reflection

The final type of interaction of a ray with an atom is a "reflection", designated by an "R". This occurs in two circumstances. If an atom is at the edge of the grid, any ray which is aimed into the grid directly beside it causes a reflection.

.........
.........
.O.....O.
.........
.........
...O.....
.........
.......O.
      RHR
      354

Rays 3 and 4 at left would each generate a reflection, due to the atom at the edge. Ray 5 would be a hit on the atom.

Double deflection

The other circumstance leading to a reflection is when two deflections cancel out. In the grid at left, ray 6 results in a reflection due to its interaction with the atoms in the grid.

   6

   R
.....
..O.O
.....
.....
.....

Detour

Rays that don't result in hits or reflections are called "detours". These may be single or multiple deflections, or misses. A detour has an entry and an exit location, while hits and reflections only have an entry location for a hit, and a single entry/exit location for a reflection.

  8   8  
.........
.........
.O.....O.
.........9
.........
...O.....
.........9
.......O.

Of course, more complex situations result when these behaviors interact. Ray 8 results in two deflections, as does ray 9.

Some rays travel a twisted course, like ray 1 at left.

 .........
 .........
 .O.....O.
 .........
1.........
 ...O.....
 .........
 .......O.
     1

Notice that this complex set of five deflections above looks exactly like a single deflection, as shown by ray 2 at left. Things are not always as simple as they seem within a black box.

 .........
 .........
 .........
 .....O...
1.........
 .........
 .........
 .........
     1

Reflections and hits can be more complex, too. Ray 2 gets deflected by the first atom, reflected by the next two atoms and again deflected by the original atom, yielding a reflection.

.O...
.....R 2
.....
.O.O.
.....

Ray 3 below gets deflected by the first atom, then by the second atom, and then hits the third atom, yielding a hit.

   ...O.
   O....
   .....
3 H.....
   ...O.

Explanation in ASCII Art:

  8   8  
..v...^..
..\>>>/.
.O.....O.
......../9
........v
...O....v
........\9
.......O.
 
 .........
 .........
 .O.....O.
 ../>>>\..
1>>/...v..
 ...O..v..
 ..../</..
 ....v..O.
     1
 
 .........
 .........
 .........
 .....O...
1>>>>\....
 ....v....
 ....v....
 ....v....
     1

Test cases

O....
...O.
..O..
.....
....O
->
 HRHH1
H     1
R     H
H     H
2     R
H     H
 H2HRH

O
->
 H
H H
 H

OOO
O O
OOO
->
 HHH
H   H
H   H
H   H
 HHH

..O..
O...O
..O..
->
 HRHRH
R     R
H     H
R     R
 HRHRH

...
...
...
->
 123
4   4
5   5
6   6
 123

....
O...
...O
....
->
 H1HH
R    1
H    R
R    H
2    R
 HH2H
```
\$\endgroup\$
8
  • \$\begingroup\$ The ascii art output wouldn't work if you had more than 10 detours. Worth noting what to do in that case. Also, fwiw, I'd vote for any reasonable output to be acceptable -- any consistent ordering, one flat list, etc... \$\endgroup\$
    – Jonah
    Mar 10, 2022 at 3:06
  • \$\begingroup\$ Test case 1, entry from right, row 1 (0-indexing): Would get caught in an infinite loop at position (1,1), no? \$\endgroup\$
    – Jonah
    Mar 10, 2022 at 3:15
  • \$\begingroup\$ @Jonah You can assume that there is only 9 detours \$\endgroup\$
    – Fmbalbuena
    Mar 10, 2022 at 12:53
  • 3
    \$\begingroup\$ I think this challenge could benefit greatly if you show the actual paths in the examples. It took me a while to understand how the reflection worked.. For anyone else confused, here the Deflection and Detour examples above with the paths in the grid (with the / and \ being how it reflects and continues traveling). For me it helped to see the 3x3 blocks surrounding the os as single atoms. \$\endgroup\$ Mar 10, 2022 at 13:44
  • 1
    \$\begingroup\$ (Although all of the rays are of course bidirectional, so you can also follow those paths in reverse.) \$\endgroup\$
    – Neil
    Mar 10, 2022 at 16:42

2 Answers 2

4
+50
\$\begingroup\$

Charcoal, 171 141 bytes

≔⁺⁺ψ⭆θ ψη⊞υηWS⊞υ⪫  ιυη≔⁰ζF⊕LυFLη«Jκι¿⁼ KK«≔⁻⁶⊗⌕KVωδM✳δW⁼KK.«≔⊗E²№KD²✳⁺δ⁺²×⁴μOε≧⁺⊗⌈εδM✳δ≧⁻↨ε±¹δ»≡KKO≔Hδ¿∧⁼ⅈκ⁼ⅉι≔Rδ«≔Iζδδ≦⊕ζ»Jκιδ»»J¹¦¹UOLθ⊖Lυ 

Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation:

≔⁺⁺ψ⭆θ ψη⊞υη

Create a header row.

WS⊞υ⪫  ι

Read the grid and pad the sides.

υη

Output the grid and a footer.

≔⁰ζ

Start with no rays.

F⊕LυFLη«

Loop over all of the squares.

Jκι¿⁼ KK«

Is this an entry point?

≔⁻⁶⊗⌕KVωδ

Work out which direction the ray leaves this entry point.

M✳δ

Move onto the grid.

W⁼KK.«

Repeat while the current cell is empty.

≔⊗E²№KD²✳⁺δ⁺²×⁴μOε

Check the neighbours for Os.

≧⁺⊗⌈εδ

If there are any then reverse direction so that the next move takes a step back. (This also handles the case of an O at the edge of the grid.)

M✳δ

Take one step.

≧⁻↨ε±¹δ

Adjust the direction if there was only one adjacent O.

»≡KKO≔Hδ

Did we hit an atom?

¿∧⁼ⅈκ⁼ⅉι≔Rδ

Were we reflected?

«≔Iζδδ≦⊕ζ»

Otherwise, output the next ray number at the exit point we just reached.

Jκιδ

Output the result of the ray at its entry point.

»»J¹¦¹UOLθ⊖Lυ 

Clear out the grid.

\$\endgroup\$
1
\$\begingroup\$

Python3, 935 bytes:

E=enumerate
S=lambda b,x:(x+1==len(b)or x==0)*[1,-1][x>0]
def F(b,C,v,x,y,q,w,c=0):
 if int==type(b[x][y])and c:b[x][y]=[v,'R'][C==(x,y)];return
 if b[x][y]=='O':b[C[0]][C[1]]='H';return
 T=[]
 for i in[1,-1]:
  X=x+(i*(q==0)+(q!=0)*q);Y=y+(i*(w==0)+(w!=0)*w)
  if X>=0 and Y>=0:
   try:T+=[i]*(b[X][Y]=='O')
   except:1
 T*=b[x+q][y+w]=='.'
 if not c and T:b[C[0]][C[1]]='R';return
 if T:
  Q,W=0,0
  if len(T)==2:Q=q-1*(q!=0)*q;W=w-1*(w!=0)*w
  else:Q=(q==0)*T[0]*-1;W=(w==0)*T[0]*-1
  F(b,C,v,x,y,Q,W,c+1);return
 F(b,C,v,x+q,y+w,q,w,c+1)
def g(b):
 for x,j in E(b):
  for y,k in E(j):
   if k and type(k)==int:F(b,t:=(x,y),b[x][y],*t,S(b,x),S(b[0],y))
 return b
K=lambda b:'\n'.join(''.join(map(str,[[j,' '][y in[0,len(a)-1]]for y,j in E(a)]if i in[0,len(b)-1]else[[' ',j][y in[0,len(a)-1]]for y,j in E(a)]))for i,a in E(b))
f=lambda b,c=0:K(g((A:=[[0]+[(c:=c+1)for _ in b[0]]+[0]])+[[(M:=(c:=c+1))]+i+[M]for i in b]+eval(str(A))))

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Hey, Can you explain? This is optional, but I can't understand \$\endgroup\$
    – Fmbalbuena
    Mar 10, 2022 at 19:00
  • \$\begingroup\$ @Fmbalbuena The code pads the input rectangle with integers (the rays), forming a gutter around the board, and then fires each ray at the sides of the box (g). F simply follows the hit/reflection/deflection etc. rules in the OP, updating the ray's original position in the gutter with the proper value (H, R, an integer), once the ray has finished bouncing around the box. \$\endgroup\$
    – Ajax1234
    Mar 10, 2022 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.