Animate Adve the Adventurer
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' <<< "### #####
## #####
## ######
## #
####### #
# ### #
# # # #
# # ###
# #######
# ####
#### ####
#### ###
##### ###
##### ###"
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>