15
1
Or maybe it's not really a labyrinth, but still.
Rules:
Input is a two-line string, consisting of
*
,1
,x
andX
. 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 lowercasex
here. 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".
Examples follow.
Input:
x *
As said before, you start before the first symbol of the first line. The first step moves you on letter
x
and this letter switches you to the second line. The letterx
no longer functions asx
, 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.
Output
2
.Input:
xX* x1*
1st step: you move on
x
, which moves you on thex
of 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 longerx
there, 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 contains1
).3rd step: you move forward, exiting the string.
Output:
3
.
Test cases:
Input:
*1* xxx
Output:
3
. (because1
makes you jump on the third position). There you never visit the second line, but it's required part of the input.Input:
*X*1*x x*1xx*
Output:
4
.Input:
1x1x ***X
Output:
3
.Input:
1*x1xxx1*x x*x1*11X1x
Output:
6
.Input:
xXXXxxx111* **xxx11*xxx
Output:
6
.
An empty string should not be a valid input, as it's not a two line string – edc65 – 2016-07-12T10:15:18.907
@edc Haha, I'm contradicting myself. Yes, indeed. – nicael – 2016-07-12T10:22:04.720
"\n\n"
is a two line string... – feersum – 2016-07-12T12:53:25.187@feersum then I think output should be
1
, as you start before the 1st line, then you move forward one step, and then you finish the labyrinth... – Amit Gold – 2016-07-18T10:58:53.343