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.
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
The room has to be built up of the following characters:
|for horizontal walls
-for vertical walls
+-----+ | | | | +-----+
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
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.
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
You can decide if you return a string or directly output to
It has to include the room's wall and the light beam (defined by the ASCII characters above).
- Standard loopholes are forbidden.
- This is code-golf, 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.
[5, 4, 2, 0, /]
+-/-+ |/ /| |\/ | +---+
[20, 3, 0, 1, \]
+------------------+ \/\/\/\/\/\/\/\/\/\| +------------------+
[10, 8, 0, 3, \] (Example of an infinite loop that won't leave the room again.)
+--------+ |/\/\/\/\| |\/\/\/\/| \/\/\/\/\| |\/\/\/\/| |/\/\/\/\| |\/\/\/\/| +--------+