Or maybe it's not really a labyrinth, but still.
Input is a two-line string, consisting of
X. That string is a labyrinth to walk through. The lines have the equal length.
You could take the input as a string with
,(comma) or any convenient separator between these two lines. Or you could take both lines as separate arguments to your function.
Output is the number of steps you have to take to exit the string (last step is the step which moves you out of the string).
You start in the top left corner (the higher line), before the first symbol.
For each step, you move forward by one symbol (from nth to (n+1)th position). Then, depending on the character you step on, the outcome is different. Here's what each char does:
*- nothing. You just step on it normally.
x- once you stepped on it, switch the line, but remain on the same horizontal distance from the beginning. For example, you stepped on the third position of the higher line, and met a lowercase
xhere. Then you immediately move to the lower line, but again at the third position.
X- switch the line and go to the next position. The example is the same there, but you also move from the third to the forth position (thus you're on the second line at the forth position).
1- just move forward by yet another position.
Once each character does its job, it's replaced with a space and no longer "works".
As said before, you start before the first symbol of the first line. The first step moves you on letter
xand this letter switches you to the second line. The letter
xno longer functions as
x, but replaced with
*. This will be more relevant in the latter examples. You're now on an asterisk on the lower line, and it did nothing to you.
The second step is moving you forward and you exit the string, so the labyrinth is completed, and it took 2 steps.
1st step: you move on
x, which moves you on the
xof the lower line. Here comes the rule which says that the used character is replaced with asterisk. Then you move back on the first line, but it's no longer
xthere, since it has been used and became an asterisk. So you move safely on this asterisk and the step is completed (you are now on the first position of the first line).
2nd step: you move on
X, it pushes you to the lower line and then pushes you forward. You now reside on the third position of the second line (asterisk), having never visited the second position (which contains
3rd step: you move forward, exiting the string.
1makes you jump on the third position). There you never visit the second line, but it's required part of the input.