12
\$\begingroup\$

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 .

Output

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 , so shortest code in bytes wins!

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

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' <<< "### #####
##  #####
## ######
##      #
####### #
#   ### #
# # #   #
# #   ###
# #######
#    ####
#### ####
####  ###
##### ###
##### ###"
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I think it's the best algorithm but it can still be golfed down by more than 20 bytes... \$\endgroup\$ – Ton Hospel Nov 1 '16 at 20:09
  • \$\begingroup\$ @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. \$\endgroup\$ – Dada Nov 1 '16 at 20:36
  • \$\begingroup\$ @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 ;) ) \$\endgroup\$ – Dada Nov 1 '16 at 20:41
  • \$\begingroup\$ 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 \$\endgroup\$ – Ton Hospel Nov 1 '16 at 21:09
  • \$\begingroup\$ @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! ) \$\endgroup\$ – Dada Nov 1 '16 at 21:22
3
\$\begingroup\$

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>

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Uh, silly me, thanks @ETHproductions \$\endgroup\$ – edc65 Nov 1 '16 at 22:10
  • \$\begingroup\$ Great work. I probably would've ended up around 160 bytes \$\endgroup\$ – ETHproductions Nov 1 '16 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.