兰顿的蚂蚁ASCII艺术。


22

画出兰顿蚂蚁的路。

描述

平面上的正方形被涂成黑色或白色。我们任意地将一个正方形标识为“蚂蚁”。蚂蚁在采取的每个步骤中都可以沿四个基本方向中的任何一个行进。蚂蚁按照以下规则移动:

  • 在白色正方形处,向右旋转90°,翻转正方形的颜色,向前移动一个单位
  • 在黑色正方形处,向左旋转90°,翻转正方形的颜色,向前移动一个单位

技术指标

  • 输入:0到725(含)之间的整数N。
  • 输出:一个17 x 17的网格,代表步骤N时蚂蚁的“路径”。

规则

  • 蚂蚁开始朝右(3点钟)。
  • 蚂蚁从网格的中心开始。
  • 分别_#@用于白色方块,黑色方块和蚂蚁。
  • 网格最初是完全白色的。
  • 您可以使用解释语言制作完整的程序或函数。
  • 通过标准输入或参数输入。

例子

更新:案例的N = 450输出错误。

N = 0

_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________

N = 1

_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________#________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________

N = 450

_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________

@Joey:是的“完整的程序或函数。通过参数或标准输入。”,自由式:)
Eelvex 2011年

@Joey:很抱歉,如果不清楚,您可以使用解释语言编写功能,也可以使用完整的程序。您可以从stdin接受输入或将其作为参数提供。
Eelvex 2011年

@Joey:请注意,在第1步中,蚂蚁首先向右转(现在朝北),然后前进。您确定要考虑到这一点吗?
Eelvex 2011年

@Joey:是的,我在上一条评论中指的是南方,您是对的,最后一个示例是针对其他N:-/的(更新了示例部分)。
Eelvex 2011年

Answers:


10

GolfScript-67个字符

~17.'_'*n+*\153:|;{|/()[124^.2/6+:6.1&17*)\2&(*|+:|;]@++}*|/();'@'@

hallvabo的Python解决方案与此最相似,因此我仅描述主要区别。

该板存储为字符串而不是数组。这样一来,我们就可以用更少的字符(因为字符串始终是扁平的)来更新板上的值,因此将其轻松转换为所需的输出格式非常容易。

蚂蚁位置通过公式((d&1)*17+1)*((d&2)-1)(即.1&17*)\2&(*)递增,其中d是方向。我们使用变量,6因此可以跳过初始化。


1
噢,现在我感觉像是GolfScript菜鸟。
aaaaaaaaaaaaaa

:6-太时髦了。我不希望调试您的代码:-)
John Dvorak 2013年

9

Ruby 1.9,104个字符

f=->z{l=[*[r=1]*17,2]*17;c=152;z.times{c+=r=(r*r>1?r/18:-r*18)*l[c]*=-1};l[c]=0;l.map{|a|putc"@_
#"[a]}}

通过函数参数输入。

  • (146-> 142)内联 m
  • (142-> 140)检查r*r>1而不是r.abs>1
  • (142-> 128)String#scan用于生成输出。更改==>
  • (128-> 125)删除过时的变量
  • (125-> 122)替换String#tr为有条件的
  • (122-> 122)现在生成与更新后的示例相同的输出
  • (122-> 111)生成蚂蚁的路径时,请使用ints而不是chars。
  • (111-> 109)重新排序某些表达式以保存括号
  • (109-> 108)代码现在是一个函数
  • (108-> 104)分别打印每个字符

功能允许的。
Eelvex 2011年

@Eelvex:函数是否必须返回字符串,或者必须输出字符串?
Ventero 2011年

输出。
Eelvex 2011年

6

Python,123

n =输入()
d = x = 152
g =(17 * [95] + [10])* 17
而n:d + = g [x] / 2; g [x] ^ = 124; x + =(1,-18,-1,18)[d%4]; n- = 1
g [x] = 64
打印“%c” * 306%tuple(g)

http://golf.shinh.org/p.rb?Langtons+Ant稍微修改了我的Python解决方案。


5

高尔夫脚本96 94 89

我最喜欢的仇恨语言是另一种半可读的sorta字节码。

89版本,我终于设法将@集成到输出循环中。

~289[0:c]*145:b;{.b>\b<)!..c++(4%:c[1 17-1-17]=b+:b;@++}@*{{(b(:b!.++'_#@@'1/=\}17*n\}17*

94版本:

~306[0:c]*152:b;{.b<\b>(!..c++(4%:c[1 18-1-18]=b+:b;\++}@*{{('_#'1/=\}17*(;n\}17*].b<\b>(;'@'\

评论:

               #Initialization.
~                  #Parse input.
306[0:c]*          #Make array of 306 0s, set c to 0 in the middle of that operation.
152:b;             #Set b to 152, remove 152 from the stack.
                   #b is a value for the ant's position, c for its rotation.

               #Run the algorithm.
{                  #Start of block.
    .b<\b>(        #Split the array at index b into before, after and value at b.
    !..            #Not the value and make 2 copies of it.
    c++            #Add the 2 copies to c.
    (4%:c          #Subtract 1, modulus by 4 and save the result to c.
    [1 18-1-18]=   #Define an array and take element number c.
    b+:b;          #Add b to the value, save result to b, remove result from stack.
    \++            #Reform the array.
}@*                #Switch the input to the top of the stack and run the block input times.

               #Convert array of 1s and 0s to the correct characters.
{                  #Start of block.
    {              #Start of block.
        ('_#'1/=   #Take the first array element, convert it to either '_' or '#'.
        \          #Switch the array to the top of the stack.
    }17*           #Execute block 17 times.
    (;n\           #Discard the 18th element of the line, write a lineshift.
}17*               #Execute block 17 times.

               #Insert the @.
]                  #Put everything in an array.
.b<\b>(            #Split the array at index b into before, after and value at b.
;'@'\              #Ditch the value at b, write a @ and shift it into place.

编辑,我不妨做一个大版本,这里有59 * 59和10500次迭代:

~59:a.*[0:c]*1741:b;{.b>\b<)!..c++(4%:c[1 a-1-59]=b+:b;@++}@*{{(b(:b!.++'_#@@'1/=\}a*n\}a*

___________________________________________________________
___________________________________________________________
_________________________##__##____________________________
________________________#__@_###___________________________
_______________________###__#_#_#__________________________
_______________________#####_#__##_________________________
________________________#___##_##_#________________________
_________________________###___#__##_______________________
__________________________#___##_##_#______________________
___________________________###___#__##_____________________
____________________________#___##_##_#__##________________
_____________________________###___#__##__##_______________
______________________________#___##_##__##___#____________
________________________####___###___#___#__###____________
_______________________#____#___#___##_####___#____________
______________________###____#___#_#______#_##_#___________
______________________###____#_##_____#_##__#_##___________
_______________________#____#___##_#_#_____##______________
_______________________#_#______#_#####__#___#_____________
______________________#___#####__________##_######_________
______________________###__##__#_##_#_#_#___##_#_##________
____________________##__#_#######_#___#__###____##_#_______
___________________#__#__######_##___#__#_##___#___#_______
__________________#____#_#_##_#__######_#######___#________
__________________#_####_##_#_####____##__##_#_##_#________
___________________#____####___#__#_######_##____###_______
______________________#___#_##_#_###_#__##__##___###_______
_________________________#######____#__##_##_#_____#_______
_________________####__##_##__####_##_##_##__#_____#_______
________________#____#_#___###_##_###____#_####____#_______
_______________###_______###_#_#_#####____#_#______#_______
_______________#_#___###_####_##_#___##_###_##_____#_______
_____________________##_##__####____####_#_#_#_____#_______
________________#____#__##___###__###_____###______#_______
________________##___##_###_####__#______###___##__#_______
________________##_#_####_____#___#__#_##_###_##___#_______
_______________####_##___##_####__#_#__#__#__###___#_______
_______________#_##_###__#_#_##_#_#_____#_#_____#_#________
___________________#_#__#____##_##__#_#__###_##____________
___________________##_#____#__#####_#____#____#__#_#_______
__________________#_##_#__#____##_##_#__###______###_______
________________#_#___#__#__#__#__###___##__##____#________
_______________###_#_#####_######_###_#######_#_##_________
_______________#_#_#____#####___##__#####_#####____________
_________________#__##___#______#__#_##__###_###___________
______________####___#####_#########___#_#_________________
_________##____#__#_____###_#_#___#_###__###_______________
________#__#__####_##___###_##___###_##_____##_____________
_______###____#_##_#_#####___#____#__#__##_###_____________
_______#_#####_#_#___##__##_____#____#___#__#______________
___________######_####__##_#___#__##__#_#_##_______________
_________##______#_###_##__####___#___###__________________
__________#__#_#####__#___#_##___#__#__#___________________
__________##_###_#######_____#_____#_##____________________
_________#_#__##_##______#___##____#_______________________
________#__#_####________###__##__#________________________
________#_##_###____________##__##_________________________
_________##________________________________________________
__________##_______________________________________________

5

Windows PowerShell,119 118

for($p,$n,$g=144,+"$args"+,1*289;$n--){$d+=$g[$p]*=-1
$p+='B0@R'[$d%4]-65}$g[$p]=0
-join'@_#'[$g]-replace'.{17}',"$&
"

4

PHP,350 309 307 312 174 161 166 159 151 149 147 144 143

<?$p=144;while($i=$argv[1]--){$g[$p]=$a=2-$g[$p];$d+=--$a;$p+=(1-($d&2))*(1+16*($d&1));}while($i++<288)echo$i%17?$i!=$p?$g[$i]?"#": _:"@":"\n";

不打高尔夫球

$p = 144; // Set initial pointer

while($i = $argv[1]--){ // Ends at -1
    $g[$p] = $a = 2 - $g[$p]; // Either returns true (2) or false(0)

    $d += --$a; // Adds 1 (2-1) or removes 1 (0-1) from the direction

    $p += (1 - ($d & 2)) * (1 + 16 * ($d & 1));
}

while($i++ < 288)
    echo $i % 17? $i != $p? $g[$i]? "#" : @_ : "@" : "\n"; // Prints the correct character

350-> 309:带有for()循环的各种压缩技术也进行了更新,以显示正确的输出。
309-> 307:将主for()循环转换为while()循环。
307-> 312:忘记将其更改为使用argv。312-
> 174:根据另一个答案重新编码。
174-> 161:不再默认整个数组。
161-> 166: Argv再次获胜。
166-> 159:无需重新定义argv [1]。
159-> 151:不再默认任何东西,PHP自动完成。
151-> 149:删除了一组括号,删除了操作顺序。
149-> 147:缩短了最后一个for()循环,不需要大括号。
147-> 144:现在,最后一个for()循环是while()循环。
144-> 143:使用临时变量保存字符。


我看到您使用了我的网格和方向技巧,它从您的代码中删除了138个字符,太好了!
PatrickvL 2011年

4

C,166 162

这里是我的Delphi方法向C的翻译,展示了C的紧凑程度。我从fR0DDY借了条件换行技巧(感谢队友!):

g[289]={0},a=144,d,i,N;main(){scanf("%d",&N);while(N--)g[a]=2-g[a],d+=g[a]-1,a+=(1-(d&2))*(1+d%2*16);for(g[a]=1;i<289;)printf("%s%c",i++%17?"":"\n","_@#"[g[i]]);}

缩进的注释版本如下所示:

g[289]={0}, // g: The grid is initially completely white. (size=17*17=289)
a=144, // a: Ant position starts at the center of the grid (=8*17+8=144)
d, // Assume 0=d: Ant start 'd'irection faces right (=0, see below)
i,
N;
main(){
  scanf("%d",&N);
  while(N--)
    // Flip the color of the square:
    g[a]=2-g[a],
    // Turn 90° right if at an '_' space, 90° left otherwise :
    d+=g[a]-1,
    // Move one unit forward;
    //   For this, determine the step size, using the two least significant bits of d.
    //   This gives the following relation :
    //     00 = 0 =  90° = right =   1
    //     01 = 1 = 180° = down  =  17
    //     10 = 2 = 270° = left  = - 1
    //     11 = 3 =   0° = up    = -17
    //   (d and 2) gives 0 or 2, translate that to 1 or -1
    //   (d and 1) gives 0 or 1, translate that to 1 or 17
    //   Multiply the two to get an offset 1, 17, -1 or -17 :
    a+=(1-(d&2))*(1+d%2*16);
  // Place the ant and print the grid :
  for(g[a]=1;i<289;)
    printf("%s%c",i++%17?"":"\n","_@#"[g[i]]); // 0 > '_', 1='@', 2 > '#'
}

+1。我喜欢这些花招"_@#"[g[i]]a+=(1-(d&2))*(1+(16*(d&1)))
fR0DDY 2011年

(1+d%2*16)节省一些字符。
2011年

@Nabb:的确,这样可以节省4个字符,谢谢您的建议!
PatrickvL 2011年

4

德尔斐(217)

var g,a:PByte;i,d,Word;begin g:=AllocMem(306);a:=g+153;Read(i);for n:=1to i do begin a^:=2-a^;d:=d-1+a^;a:=a+(1-2and d)*(1+17*(1and d))end;a^:=1;for n:=1to 306do if n mod 18=0then WriteLn else Write('_@#'[1+g[n]])end.

缩进和注释的代码如下所示:

var
  g,a:PByte;
  i,d,n:Int32;
begin
  g:=AllocMem(306); // g: The grid is initially completely white. (size=18*17=306)
  // Assume 0=d: Ant start 'd'irection faces right (=0, see below)
  a:=g+153; // a: Ant position starts at the center of the grid (=8*18+9=153)
  Read(i);
  for n:=1to i do
  begin
    // Flip the color of the square;
    a^:=2-a^;
    // Turn 90° right if at an '_' space, 90° left otherwise;
    d:=d-1+a^;
    // Move one unit forward;
    //   For this, determine the step size, using the two least significant bits of d.
    //   This gives the following relation :
    //     00 = 0 =  90° = right =   1
    //     01 = 1 = 180° = down  =  18
    //     10 = 2 = 270° = left  = - 1
    //     11 = 3 =   0° = up    = -18
    //   (d and 2) gives 0 or 2, translate that to 1 or -1
    //   (d and 1) gives 0 or 1, translate that to 1 or 18
    //   Multiply the two to get an offset 1, 18, -1 or -18 :
    a:=a+(1-2and d)*(1+17*(1and d))
  end;
  // Place the ant and print the grid :
  a^:=1; // 0 > '_', 1='@', 2 > '#'
  for i:=1to 306do
    if i mod 18=0then // we insert & abuse column 0 for newlines only (saves a begin+end pair)
      WriteLn
    else
      Write('_@#'[1+g[i]])
end.

输入:

450

输出:

_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________

@Patrick:示例错误,请检查更新。(并且似乎您输出了步骤451 :))。
Eelvex 2011年

@Eelvex:谢谢。我以4个字符的费用修复了'N = 0'的情况...现在我要再次赢得他们!;-)
PatrickvL 2011年

@Eelvex:PS:3小时前仅凭一个谦虚的话就没有发现您的错误的+1,这可能是我的错吗?;)
PatrickvL 2011年

@Patrick:我在等待<200,但是还可以... :)
Eelvex 2011年

@Eelvex:大声笑,到达那里...(已经下降到238)
PatrickvL 2011年

3

C 195个字符

x=144,T,p=1,i,N[289]={0},a[]={-17,1,17,-1};c(t){p=(p+t+4)%4;x+=a[p];}main(){scanf("%d",&T);while(T--)N[x]=(N[x]+1)%2,c(N[x]?1:-1);for(;i<289;i++)printf("%s%c",i%17?"":"\n",i-x?N[i]?'#':'_':'@');}

http://www.ideone.com/Dw3xW

我得到725。

_________________
_________________
___________##____
____##______##___
___#___##__##_#__
__###____#_#__#__
__#_#_#__#_#_#___
______###____#__@
_______###__#__#_
_____#_#____#___#
___#_#_#_##____#_
__#__#_#_#_#_###_
__#_##_#_____####
___##_#____#_####
____###___####_#_
_______#__#__##__
________####_____

使用p+=t+4;x+=a[p%4];而不是p=(p+t+4)%4;x+=a[p];保存三个字符。
2014年

3

sed,481个字符

#n
1{s/.*/_________________/;h;H;H;H;G;G;G;G;s/^\(.\{152\}\)_/\1@/;s/$/;r/;ta;};x;:a;/;r/br;/;d/bd;/;l/bl;/;u/bu;:w;y/rdlu/dlur/;bz;:b;y/rdlu/urdl/;bz;:r;s/@\(.\{17\}\)_/#\1@/;tw;s/@\(.\{17\}\)#/#\1!/;tw;s/_\(.\{17\}\)!/@\1_/;tb;s/#\(.\{17\}\)!/!\1_/;tb;:d;s/_@/@#/;tw;s/#@/!#/;tw;s/!_/_@/;tb;s/!#/_!/;tb;:l;s/_\(.\{17\}\)@/@\1#/;tw;s/#\(.\{17\}\)@/!\1#/;tw;s/!\(.\{17\}\)_/_\1@/;tb;s/!\(.\{17\}\)#/_\1!/;tb;:u;s/@_/#@/;tw;s/@#/#!/;tw;s/_!/@_/;tb;s/#!/!_/;tb;:z;h;${s/!/@/;s/;.//p}

通过删除第一行并运行,可以减少到478个字符 -n

需要N行输入,例如。当以

seq 450 | sed -f ant.sed

输出:

_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________

3

Perl,110个字符

$p=144;$p+=(1,-17,-1,17)[($d+=($f[$p]^=2)+1)%4]for 1..<>;$f[$p]=1;print$_%17?'':$/,qw(_ @ #)[$f[$_]]for 0..288

从STDIN的第一行读取数字。其余输入将被忽略。

更具可读性:

$p=144;
$p += (1,-17,-1,17)[($d+=($f[$p]^=2)+1) % 4] for 1..<>;
$f[$p]=1;
print $_%17 ? '' : $/, qw(_ @ #)[$f[$_]] for 0..288

编辑

  • (112→111)无需$d使用4模值进行更新。

  • (111→110)现在可以内联$d增量

附录(109个字符)

如果您很乐意遇到N=0失败的特殊情况(它不会@为蚂蚁输出字符),我们可以将其缩短一个字符。所有其他输入正常工作:

$p+=(1,-17,-1,17)[($d+=($f{$p+0}^=2)+1)%4]for 1..<>;$f{$p}=1;print$_%17-9?'':$/,qw(_ @ #)[$f{$_}]for-144..144

区别在于我们现在使用%f而不是,@f因此我们可以使用负索引,并且我们从而-144..144不是进行迭代0..288。无需初始化$p


1

Mathematica,94个字符

a@_=d=1;a@Nest[#+(d*=(a@#*=-1)I)&,9-9I,Input[]]=0;Grid@Array["@"[_,"#"][[a[#2-# I]]]&,17{1,1}]

1

> <>,​​122个字节

冒着一点线程坏死的风险,我认为用> <>编写答案将是一个有趣的挑战...

1&f8r\
1-:?!\r:@@:@$:@@:@g:2*1+&+4%:&}1$-@p{:3$-5gaa*-$@+@5gaa*-+r
2}p70\~
a7+=?\:@@:@g4+5go$1+:
o053.>~1+:64*=?;a
dedc_#@

该程序希望执行之前要在堆栈上显示要计算的步骤数。

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.