## The challenge

Your task is to animate Adve the Adventurer moving through a creepy (i.e. Halloween) maze. Adve is a •; he's character fluid, though, so he does not mind being represented by a different character.

To animate Adve, you print out each frame; a frame is the map with his current location in it. Adve moves one space forward every turn and never backtracks. He starts in the first row and ends in the last.

## Input

Any reasonable format like a string with a delimiter or string array. You can assume the input will be a map greater than 3*3, containing only one possible path. The only characters present will be # and .

The frames.

## Example maze (ok... labyrinth)

Here is a map without Adve in it; the first and last frames are this empty map (this map is 9x15):

### #####
##  #####
## ######
##      #
####### #
#   ### #
# # #   #
# #   ###
# #######
#    ####
#### ####
####  ###
##### ###
##### ###
##### ###


This is , so shortest code in bytes wins!

The exact output for this can be found here (37 frames).

## This is code-golf, so shortest code in bytes wins!

• Will the first and last rows always have a single empty cell? Will there always be a single possible path (no bifurcations)? Oct 31 '16 at 23:00
• @LuisMendo, yes, and there is "only one possible path" Oct 31 '16 at 23:11
• Will the entrance always be at the top? Nov 1 '16 at 3:08
• @DestructibleWatermelon, yes, and the exit will be at the bottom. Nov 1 '16 at 11:10
• His real name is Dave, but he's all mixed up. Nov 1 '16 at 21:10

## Perl, 84 bytes

Thanks @Ton Hospel for guiding me to the right direction to golf out around 30 bytes!

Bytecount includes 82 bytes of code and -0p flags.

/.*/;say y/A/ /r;s/&(.{@{+}})? /A$1&/s||s/ (.{@{+}})?&/&$1A/s||s/ /&/?redo:y;A&;


Note that there are two final spaces, and no final newline (it won't work otherwise).

Takes the maze as input as outputs all the needed frames for Adve to get out of it. Note that Adve is a & rather than a •, since the latter isn't utf8 (and perl doesn't use utf8 by default). Run it with -0pE flags :

perl -0pE '/.*/;say y/A/ /r;s/&(.{@{+}})? /A$1&/s||s/ (.{@{+}})?&/&$1A/s||s/ /&/?redo:y;A&;  ' <<< "### #####
##  #####
## ######
##      #
####### #
#   ### #
# # #   #
# #   ###
# #######
#    ####
#### ####
####  ###
##### ###
##### ###"


Just for the eyes, I also made this animated version, that is a little bit longer, but will clear the terminal between each print and sleep 0.15 sec, so it will look like Adve is actually moving :

perl -0nE 'system(clear);/.*/;say y/A/ /r;select($,,$,,$,,0.15);s/&(.{@{+}})? /A$1&/s||s/ (.{@{+}})?&/&$1A/s||s/ /&/?redo:say"\e[H",y/A&/ /r' <<< "### ##### ## ##### ## ###### ## # ####### # # ### # # # # # # # ### # ####### # #### #### #### #### ### ##### ### ##### ###"  • I think it's the best algorithm but it can still be golfed down by more than 20 bytes... Nov 1 '16 at 20:09 • @TonHospel -9 bytes so far (I removed the $s="@+", I hadn't realize earlier that @+ is only changed if a successful regex occurs. And redo instead of while saved one or two bytes). Any hint about how to golf it more? I'm guessing I have to get rid of those y/// somehow, or make the s/// shorter... but either way I don't know how.
Nov 1 '16 at 20:36
• @TonHospel (but if you worked on a solution and want to post it, don't hold it because it's the same algorithm or something, I won't mind at all ;) )
Nov 1 '16 at 20:41
• How regex variables are or aren't kept in loops is very subtle. The y/// is fine since you need something to indicate direction (but notice you get to choose on which side) But the main improvement will come from combining substitutions Nov 1 '16 at 21:09
• @TonHospel Indeed, I should've seen it, but I was trying too hard to combine s/ &/&A/ and s/& /A&/ together (and the next to together) to see that those weren't the regex I needed to combine! Thanks a lot! (And thanks for letting me find out how to golf it! )
Nov 1 '16 at 21:22

# JavaScript (ES6), 137

(1 byte saved thx @ETHproductions)

m=>(o=>{for(p=m.search -o,r=[m];[d,o/d,-o/d].some(q=>1/m[d=q,q+=p]?p=q:0);r.push(q.join))(q=[...m])[p]=0})(d=1+m.search
)||[...r,m]


Less golfed

m=>{
d = o = 1+m.search\n; // offset to next row and starting direction
p = m.search -o; // starting position, 1 row above the first
for( r=[m]; // r is the output array, start with empty maze
// try moving in 3 directions (no back)
// if no empty cell found, we have exit the maze
[d,o/d,-o/d].some(q => 1/m[d=q,q+=p]? p=q : 0);
r.push(q.join) // add current frame
)
q=[...m], q[p] = 0; // build frame, '0' used to mark Adve position
return [...r,m] // add last frame with maze empty again
}


Test

F=
m=>(o=>{for(p=m.search -o,r=[m];[d,o/d,-o/d].some(q=>1/m[d=q,q+=p]?p=q:0);r.push(q.join))(q=[...m])[p]=0})(d=1+m.search\n)||[...r,m]

function go() {
var i=I.value,r=F(i),
frame=x=>(x=r.shift())&&(O.textContent=x,setTimeout(frame,100))
frame()
}

go()
#I { width:10em; height: 19em; font-size:10px}
#O { white-space:pre; font-family: monospace; font-size:10px; vertical-align: top; padding: 4px}
<table><tr><td>
<textarea id=I>### #####
##  #####
## ######
##      #
####### #
#   ### #
# # #   #
# #   ###
# #######
#    ####
#### ####
####  ###
##### ###
##### ###
##### ###
</textarea><button onclick='go()'>go</button></td><td id=O></td></tr></table>

• Uh, silly me, thanks @ETHproductions Nov 1 '16 at 22:10
• Great work. I probably would've ended up around 160 bytes Nov 1 '16 at 22:17