Walk the labyrinth
Snails, 34 bytes
A^
\1r|\xud|\Xaa7},(\*|\xud=\x)r},
Expanded:
{
{
\1 r |
\x ud |
\X aa7
},
(\* | \x ud =\x)
r
},
For a path that takes N steps, the program finds one successful match for each traversal of 0 steps, 1 steps, ..., N - 1 steps.
Haskell, 68 66 65 bytes
(a:b)#l@(c:d)|a<'+'=1+b#d|a>'w'=l#('*':b)|a>'W'=d#b|1<2=b#d
_#_=1
Function #
takes both lines as separate parameters. Usage example: "1x1x" # "***X"
-> 3
.
We just have to count the stars *
we step on plus 1 for leaving.
(a:b)#l@(c:d) -- bind: a -> first char of first line
b -> rest of first line
l -> whole second line
c -> first char of second line (never used)
d -> rest of second line
|a < '+' = 1+b#d -- stepped on a *, so add 1 and go on
|a > 'w' = l#('*':b) -- x switches lines and replaces the x with *
|a > 'W' = d#b -- X switch lines and go on
|1<2 = b#d -- the rest (-> 1) simply walks forward
_#_=1 -- base case: the empty string counts 1 for leaving
Edit: @feersum saved a byte. Thanks!
JavaScript (ES6), 119
l=>{z=-~l.search`
`,l=[...l+' '];for(n=p=0;(c=l[p%=2*z])>' ';p+=c>'X'?z:c>'1'?z+1:c>'0'?1:(++n,1))l[p]='*';return-~n}
Less golfed
l=>{
z=1+l.search`\n`;
l=[...l+' '];
for( n = p = 0;
(c=l[p%=2*z])>' ';
p += c>'X' ? z : c>'1' ? z+1 : c>'0'? 1 : (++n,1) )
l[p] = '*';
return 1+n
}
Test
f=l=>{z=-~l.search`
`,l=[...l+' '];for(n=p=0;(c=l[p%=2*z])>' ';p+=c>'X'?z:c>'1'?z+1:c>'0'?1:(++n,1))l[p]='*';return-~n}
[['x\n*',2]
,['xX*\nx1*',3]
,['*1*\nxxx',3]
,['*X*1*x\nx*1xx*',4]
,['1x1x\n***X',3]
,['1*x1xxx1*x\nx*x1*11X1x',6]
,['xXXXxxx111*\n**xxx11*xxx',6]
].forEach(t=>{
var i=t[0],k=t[1],r=f(i)
console.log('Test result '+r+(r==k?' OK ':' KO (expected '+k+')')+'\n'+i)
})