Input
Your input is a single string, separated by newlines into 2n+1
lines of length 2n+1
, for some integer n ≥ 0
. The integer n
is not part of the input; you'll have to compute it from the string. The lines are composed of the "direction characters" >^<v
. If newlines pose a problem, you can replace them by vertical pipes |
.
The input forms a square grid of size (2n+1)x(2n+1)
, and each cell of the grid is interpreted as a rotor router, which points in one of the four cardinal directions. We proceed to drop a token on the router at the center of the grid, and then the routers will move it around in the following way. When the token lands on a router, the router turns 90 degrees in the counter-clockwise direction, and moves the token one step in the new direction it points to. If it lands on another router, the process is repeated, but eventually, the token will fall off the grid.
Output
Your output is the final configuration of the routers, in the same format as the input.
Example
As an example input, consider the 3x3
grid
<^<
^><
>^v
where the central router has been highlighted to indicate the token (it's a bit hard to see). The central router rotates to face north, and moves the token to the central cell of the top row:
<^<
^^<
>^v
This router rotates to face west, and sends the token to the top left corner:
<<<
^^<
>^v
The router in the corner sends the token south, so it's now at the leftmost cell of the middle row:
v<<
^^<
>^v
That router rotates to face west and sends the token off the grid.
v<<
<^<
>^v
This is the final grid configuration, so your program should output it. Note that in more complex examples, the token can pass the same router multiple times before falling off the grid.
Rules
You can write either a function or a full program. This is code-golf, so the lowest byte count wins. Standard loopholes are disallowed. You can decide whether there is a trailing newline in the input and/or output.
Test Cases
Input:
v
Output:
>
Input:
<^<
^><
>^v
Output:
v<<
<^<
>^v
Input:
>>^>>
v<vv<
>^>^<
^<>>^
vvv>>
Output:
>>^>>
>v>>v
^>>vv
^^>>>
v^<<^
Input:
<^^^^^^^^
<<^^^^^^>
<<<^^^^>>
<<<<^^>>>
<<<<^>>>>
<<<vv>>>>
<<vvvv>>>
<vvvvvv>>
vvvvvvvv>
Output:
>>>>>>>>v
^>>>>>>vv
^^>>>>vvv
^^^>>vvvv
<<<<<vvvv
^^^^<<vvv
^^^<<<<vv
^^<<<<<<v
^<<<<<<<<