16
This challenge is a prize for ais523 for winning the "Rookie of the Year" category in "Best of PPCG 2016". Congratulations!
BackFlip is an esoteric programming language made by user ais523, who has created well over 30 other interesting esolangs.
BackFlip is a 2D language like Befunge or ><> where the instruction pointer traverses a grid of text (the program), moving up, down, left, and right, changing direction depending on the character it's on. Critically, the grid in a BackFlip program changes as it is being traversed, a bit like Langton's Ant.
For this challenge you may assume that a BackFlip program is always a rectangular grid of text (all lines the same length), 1×1 in size at minimum, only containing the characters ./\<>^V
. (.
is used for visibility rather than space.) Semantically the BackFlip we'll use here is identical to the original spec.
The instruction pointer (IP) in BackFlip always starts just left of the top-left corner of the program, heading right. There are three types of commands it can encounter:
.
is a no-op. The IP continues on in the direction it was going. The no-op stays a no-op./
and\
are mirrors. They reflect the IP in the direction indicated by their angle, then they change into the other type of mirror.- For example, if the IP heads left into a
\
, it starts moving upward instead of left and the\
becomes a/
.
- For example, if the IP heads left into a
<
,>
,^
, andV
are arrows. They redirect the IP to the direction they point in, then they change into an arrow that points in the direction the IP came from (opposite the direction the IP was moving).- For example, if the IP heads downward into
>
, it starts moving right instead of downwards and the>
becomes a^
because that is the direction the IP came from.
- For example, if the IP heads downward into
A BackFlip program terminates when the IP moves out of bounds, i.e. goes off the grid. It turns out all BackFlip programs eventually end because infinite loops are impossible. (You may assume this is true.)
Your goal in this challenge is to write a program or function that takes in a BackFlip program and outputs the number of moves the instruction pointer takes before the program terminates. That is, how many steps does the IP take in the course of running a program? This includes the initial step onto the grid and the final step off of it.
For example, the instruction pointer takes 5 steps in the trivial grid ....
:
.... <- empty 4×1 grid
012345 <- step number of the IP
So the output to ....
is 5
.
In the more complex 4×2 grid
\...
\.><
the IP exits the grid on its 9th step, so the output is 9
:
step grid IP position (@)
0 \... @....
\.>< ....
1 \... @...
\.>< ....
2 /... ....
\.>< @...
3 /... ....
/.>< .@..
4 /... ....
/.>< ..@.
5 /... ....
/.<< ...@
6 /... ....
/.<< ..@.
7 /... ....
/.>< .@..
8 /... ....
/.>< @...
9 /... ....
\.>< ....
@
The shortest code in bytes wins.
You may take input as an array of lines or matrix of characters instead of a multiline string if desired, but you must use the characters ./\<>^V
(not integer opcodes). You may use space instead of .
if preferred. It's fine if characters like \
need to be escaped in input. Output is always an integer more than one.
Test Cases
....
5
\...
\.><
9
.
2
..
3
.
.
2
\
2
^
2
.^.
3
<.
2
\\
\/
7
>V
^<
6
>\
>/
6
\><
2
\><
\><
7
\><
\><
\><
12
\.V.
\.\<
5
\.V.
\./<
9
V./\
V./\
>./<
..\/
14
\V..
.^..
\/><
.V..
.^..
20
\.V.V.
\./.\<
.>\<..
..^.^.
31
\.V.V.V.
\./>/.\<
.>\>\<..
..^.^.^.
69
\.V.V.V.V.
\./>/>/.\<
.>\>\>\<..
..^.^.^.^.
145
\.V.V.V.V.V.V.V.V.V.V.
\./>/>/>/>/>/>/>/>/.\<
.>\>\>\>\>\>\>\>\>\<..
..^.^.^.^.^.^.^.^.^.^.
9721
1It's such a shame that you can't make a BackFlip solution to this... – HyperNeutrino – 2017-04-09T02:27:21.413
Confused about the mirrors... does / flips directions as left=>up and up=>left,? – officialaimm – 2017-04-09T04:38:29.053
1@officialaimm Heading from the left into
/
will make the IP go up and heading up into/
will make the IP go right, as if it is a ball bouncing off a wall. (But remember the/
changes to backslash after the IP touches it.) – Calvin's Hobbies – 2017-04-09T06:22:27.973why '\'<LF>'/' is 7 instead of 6? – tsh – 2017-04-09T08:00:38.083
@tsh Because the mirrors flip direction after each use, and also you count both entering and exiting the grid. – Ørjan Johansen – 2017-04-09T08:07:48.393
2Looks like this is ais523's esolangs day – Leo – 2017-04-09T09:37:58.657
@ETH it changes to the direction the IP came from which is left. – Calvin's Hobbies – 2017-04-10T01:02:47.517
Oh right, my bad. I guess I got that confused with the mirrors switching every time – ETHproductions – 2017-04-10T12:55:55.137