Is the path correct ?
Given a string, your program must determine whether or not Alice can reach Bob, following the signs on the map. You must return a truthy or falsy value.
The input string will contain the following characters :
(space) and
\n
. Only used for padding.>
,<
,^
,v
, are the "signs". They show the direction.&
. Ignore next char, whatever it is (including spaces andBob
)A
andB
respectively Alice and Bob.
The map is a torus. If Alice reaches the end of a row, then she is teleported back to the beginning of that row. Same applies for columns.
Examples
Given those examples as input, your program must return a truthy value (each input is separated with a -
line):
A>B
-----------------
<BA
-----------------
A B
-----------------
BA
-----------------
Av >B
> ^
-----------------
Av
B
-----------------
A>v
>B
-----------------
A>v> v>B
v< > &<^
> ^
-----------------
A>v> v
v< >B&<
> ^
-----------------
A&<B
-----------------
A&B<
-----------------
<A &B
For the following, your program must output a falsy value or nothing :
AvB
-----------------
A&B
-----------------
A><B
-----------------
A>v
^<B
Path viewer
You can have a visual representation of the input here. Thanks to arnemart for this.
Some rules
- The input may be padded with spaces to form a rectangle
- Initially, Alice goes straight forward (from above, she goes to the right)
- Your program should handle infinite loop cases (see Hint)
A
is not always the first character, as you may have seen, however, you may write a program that handles only input withA
being the first char, but 20 bytes will be added to your score.- A map (the input) is considered invalid in case there are other characters than those specified above. Your program won't have to handle them.
- This is code-golf, so shortest code in bytes wins.
Hint
Your program will most likely be a loop, unless you find a more mathematical solution to this challenge. To handle infinite path, I recommend using a finite loop (e.g for
) instead of a potentially infinite one (e.g while
).
Therefore, the maximum iteration of the loop I can think of (I did not do any maths, any correction is welcome) is (length of input)*4