Given a set of closed non-overlapping 2d contours (separated by at least one space even on diagonals) with arrows oriented consistently in the same clockwise or counter-clockwise direction (each contour has its own direction) and a positive number n
, move the arrows n
steps along the contours in the respective direction.
The arrows are represented by > v < ^
respectively for right, down, left and up directions. There the other characters are -
(horizontal), |
(vertical) and +
(corner).
When an arrow is on a corner, it keeps its current direction and changes it only after the turn is taken.
There will always be a straight segment (or a space) between any two corners (like +-+
for the horizontal and similar for the vertical) - in other words the sharp U
turns are forbidden. The segments between the corners are either vertical or horizontal and the bend at a corner is always 90 degree.
Input:
- a positive integer -
n
- number of steps - an ASCII representation of the contours - it can be a multiline string, a list of strings, a list of characters and so on.
Output:
The same contours with all arrows shifted n
steps in each contour's overall direction.
Test cases:
1.
Input:
n
= 1
+----->->
| |
| v---+
| |
+---<-------+
Output:
+------>+
| v
| +>--+
| |
+--<--------+
2.
Input:
n
= 2
+-----+ +---+
| | | |
+-->--+ | v
| |
+--->---+ |
| |
+------<<---+
Output:
+-----+ +---+
| | | |
+---->+ | |
| |
+----->-+ v
| |
+----<<-----+
3.
Input:
n
= 3
+---+ +---+ +-------+
| | | v | |
^ | | | +-<-+ |
| | ^ | | v
| +---+ +-->----+ |
| |
| +-------+ +---+ |
| | | v | |
+---+ +---+ +---+
Output:
+>--+ ^---+ +-------+
| | | | ^ |
| | | | +---+ |
| | | | | |
| +---+ v----->-+ |
| |
| +-------+ +---+ v
| | | | | |
+---+ +-<-+ +---+
4.
Input:
n
= 1
+--+
| |
| +---+
| |
+----+ |
| |
+-+
Output:
+--+
| |
| +---+
| |
+----+ |
| |
+-+
5.
Input
n
= 4
^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v
Output:
^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v
6.
Input:
n
= 1
^->
^ v
<<v
Output:
^>+
^ v
<<v
Write a function or a program solving the above task. The shortest code in bytes in every language wins. Don't be discouraged by the golfing languages. Explanation of the algorithm and the code is highly appreciated.
There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden.
\$\endgroup\$