(This challenge is very similar, but this current challenge has additional complications.)

Imagine a 2-dimensional room, where the walls are plated with flat mirrors.

Suddenly, a beam of light strikes into the room, where a piece of wall is missing! The lightbeam dances around the room, getting reflected by the wall mirrors, and eventually leaving the room again.

The Logic

You are given 5 variables: W, H, X, Y and Z.
Now, what do they mean?

W, H is the size of the room (including the walls), with W being width and H being height.
X, Y is the coordinate that the wall has the hole at. You can assume that this is always located on a wall tile. Coordinates are 0-based, with the X-axis pointing right and the Y-axis pointing down.
Z is a single character, representing the direction that the light falls into the room, either \ or /.

The room has to be built up of the following characters:

  • | for horizontal walls
  • - for vertical walls
  • + for corners

Example: (W=7,H=4)

|     |
|     |

Now that we have a room, lets destroy one of the wall tiles, so a beam of light falls into the room. Beams of light are diagonal lines, represented by \ and / characters.

Let's replace the wall tile at X=2, Y=0 with a \ beam of light.

|     |
|     |

The incoming light travels diagonally across the room until it hits a mirror wall. If a wall is hit, the direction inverts along the axis of the wall, and the beam travels on.

|\ \/\|
| \/\/|

In this example, the light beam gets to a point where a corner of the wall is hit, which inflicts that the beam gets reversed completely and travels all the way back, eventually leaving the room.

Your Task

Write a program that prints the room and the whole path of the light beam, until it leaves the room again or repeats into an infinite loop.


Input may be received in any reasonable format, but it has to include the 4 integers W, H, X, Y and the character Z, for example [10, 8, 0, 3, \].

You may assume that:

  • W, H >= 3
  • X, Y are always located at a wall
  • Z will only be able to contain the values \ and /.


You can decide if you return a string or directly output to stdout.
It has to include the room's wall and the light beam (defined by the ASCII characters above).


  • Standard loopholes are forbidden.
  • This is , so the shortest code in bytes, in any language, wins.
  • All coding languages that were created prior to the publication of this challenge are allowed.


Input: [5, 4, 2, 0, /]

|/ /|
|\/ |

Input: [20, 3, 0, 1, \]


Input: [10, 8, 0, 3, \] (Example of an infinite loop that won't leave the room again.)


Good luck!


1 Answer 1


Charcoal, 55 bytes


Try it online! Link is to verbose version of code. Edit: I don't know why but that code isn't working right now. Here's a tweaked version instead. Explanation of the original code:


Draw the outer box.


Move the cursor to the entry point and make a hole in the box at this point, so this becomes the background.


Rotate to the appropriate direction to print the initial line segment.


While the cursor is over a hole, draw the next line segment.


Look for a hole in which to draw the next line segment. This works by repeating the same code three times. The first time, nothing happens. The second time, if the cursor is not over a hole, it rotates right 45°, then takes one step to the right, then rotates right 45°. This has the effect of reflecting it in one axis. The third time, if the cursor is still not over a hole, it rotates right 90°, then takes one step to the right, then rotates right 90°. This has the effect of changing the reflection to the other axis. If there still isn't a hole, then a corner or infinite loop has been reached, so the outer loop halts.

  • 1
    \$\begingroup\$ In order for people to test your code works, it would help to have a link to the golfed code online, with the verbose version separately. \$\endgroup\$ Aug 4, 2017 at 17:07
  • \$\begingroup\$ TIO works just fine with this code for me. \$\endgroup\$
    – Ian H.
    Aug 4, 2017 at 17:17
  • \$\begingroup\$ @trichoplax that was easy \$\endgroup\$
    – MD XF
    Aug 4, 2017 at 17:17
  • \$\begingroup\$ @trichoplax The TIO link does actually shows the golfed code in the output, which is where I copy and paste it from... \$\endgroup\$
    – Neil
    Aug 4, 2017 at 18:25
  • 1
    \$\begingroup\$ @trichoplax When you use -v -sl then it's actually the translated succinct code as printed that gets executed. This does of course depend on the deverbosifier generating correct succinct code, because if it doesn't, then the generated code fails, even though the original verbose code was technically correct. \$\endgroup\$
    – Neil
    Aug 5, 2017 at 23:25

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.