28
3
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.
Can two contours touch their corners on a diagonal, or a contour touch itself like that? – xnor – 2019-07-31T06:57:50.860
4"Given a set of closed non-overlapping 2d contours ... with arrows oriented consistently in the same clockwise or counter-clockwise direction" sounds to me like every contour is oriented in the same direction, whereas from the test cases, it seems the arrows only have be consistent within a contour. – xnor – 2019-07-31T07:00:56.370
3@xnor Thanks for your comments!
I'll update the description. – Galen Ivanov – 2019-07-31T07:11:34.407
@ngn No, it's a mistake - I'll correct it. Thanks for observing it! – Galen Ivanov – 2019-07-31T07:43:52.913
Can I take input as a singleline StringBuilder and an integer representing the length of each line? (language is C#) – Embodiment of Ignorance – 2019-07-31T08:35:00.070
@EmbodimentofIgnorance Yes, you can. – Galen Ivanov – 2019-07-31T08:39:53.297
2
Is input with no space between the walls possible? Eg: Try it online!. I know you said "separated by at least one space" but I was unclear if that applied only to independent loops or if it applied to a single loop as well.
– Jonah – 2019-08-01T00:21:34.7401@Jonah No, it's not possible:
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.
– Galen Ivanov – 2019-08-01T06:19:27.000@Arnauld Don't worry :) – Galen Ivanov – 2019-08-01T13:14:12.513