动画Adven the Adventurer


12

挑战

您的任务是为穿越冒险家(即万圣节)迷宫的Adve the Adventurer设置动画。Adve是一个; 不过,他性格流畅,因此他不介意由其他角色代表。

要为Adve制作动画,请打印出每一帧;框架是其中包含当前位置的地图。Adve每转一圈向前移动一个空间,永不后退。他从第一行开始,到最后一行结束。

输入值

任何合理的格式,例如带有定界符的字符串或字符串数​​组。您可以假设输入将是一个大于3 * 3的地图,仅包含一个可能的路径。出现的唯一字符是#

输出量

框架。

示例迷宫(好吧,迷宫

这是一张没有Adve的地图;第一帧和最后一帧是此空白地图(此地图为9x15):

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

这是,因此以字节为单位的最短代码胜出!

确切该输出可以找到这里(37帧)。

这是,因此以字节为单位的最短代码胜出!


第一行和最后一行将始终有一个空单元格吗?会不会总是有一个单一的路径(没有分叉)?
路易斯·门多

@LuisMendo,是的,而且“只有一条可能的道路”
Daniel Daniel

1
入口会一直在顶部吗?
破坏的柠檬

@DestructibleWatermelon,是的,出口将在底部。
丹尼尔(Daniel)

4
他的真名是Dave,但他混在一起。
mbomb007 '16

Answers:


4

Perl,84个字节

感谢@Ton Hospel指导我向正确的方向发展,大约30个字节!

字节数包括82个字节的代码和-0p标志。

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

请注意,有两个最后的空格,没有最后的换行符(否则它将不起作用)。

将迷宫作为输入作为输出,以使Adve摆脱困境。请注意,Adve是a &而不是a ,因为后者不是utf8(默认情况下,perl不使用utf8)。使用-0pE标志运行它:

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

只是为了眼睛,我还制作了这个动画版本,该版本稍长一些,但是它将清除每次打印和睡眠之间的0.15秒,因此看起来Adve实际上正在移动:

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' <<< "### #####
##  #####
## ######
##      #
####### #
#   ### #
# # #   #
# #   ###
# #######
#    ####
#### ####
####  ###
##### ###
##### ###"

我认为这是最好的算法,但仍然可以减少20多个字节……
Ton Hospel '16

到目前为止,@ TonHospel -9个字节(我删除了$s="@+",我之前没有意识到,@+只有在成功的正则表达式发生时才会更改,而redo不是while保存一个或两个字节)。关于如何更多地打高尔夫球的任何提示?我猜我必须以y///某种方式摆脱这些,或者使它变得s///更短……但是,无论哪种方式,我都不知道如何。
达达

@TonHospel(但是,如果您正在研究解决方案并想发布它,请不要持有它,因为它是相同的算法或某种东西,我一点都不介意;))
Dada

正则表达式变量如何保持或不保持循环非常微妙。该y///是好的,因为你需要的东西来指示方向(但是请注意你选择哪一方),但主要的改进将来自合并换人
吨Hospel

@TonHospel事实上,我已经看到了,但我太艰苦设法结合s/ &/&A/s/& /A&/一起(和旁边一起),一看就知道这些并非我需要结合正则表达式!非常感谢!(感谢您让我知道如何打高尔夫球!)
Dada

3

JavaScript(ES6),137

(@ETHproductions保存了1个字节)

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]

少打高尔夫球

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
}

测试

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>


呃,愚蠢的我,谢谢@ETHproductions
edc65 '16

做得好。我可能最终大约需要160个字节
ETHproductions'Nov
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.