Langton's Ant

Author

Eric W. Weisstein
January 27, 2011

This notebook downloaded from http://mathworld.wolfram.com/notebooks/ComputationalSystems/LangtonsAnt.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/LangtonsAnt.html.

©2010 Wolfram Research, Inc. except for portions noted otherwise

Additional Authors

Based on ideas in Stephen Wolfram's A New Kind of Science

Some figures adapted from Stephen Wolfram's A New Kind of Science

Legacy documentation for NKSExplorer package available at https://reference.wolfram.com/legacy/applications/nksx/

Ant

Ant[x_:{0,0},o_:I]:=With[{y=-.05,m=RotationMatrix[-Arg[o]]},
{Red,
Circle[x+{0,0},.5],
Disk[x+m.{0,.3+y},.08],
Disk[x+m.{0,.1+y},Abs[m.({.07,.1}1.4)]],
Disk[x+m.{0,-.15+y},Abs[m.({.08,.1}1.4)]],
{
Line[{x+m.{0,.3+y},x+m.{-.1#,.4+y}}],
Line[{x+m.{0,0.05},x+m.{.2#,.15}}],
Line[{x+m.{0,.05},x+m.{.22#,.05}}],
Line[{x+m.{0,0.05},x+m.{.2#,-.05}}]
}&/@{-1,1}
}
]

Show[Graphics[Ant[{0,0}]],AspectRatio->Automatic]

Code

TM2DStep[rule_,{s_,tape_,r:{x_,y_}}]:=Apply[{#1,ReplacePart[tape,#2,{r}],r+#3}&,{s,tape[[x,y]]}/.rule]

LangtonsAnt[size_List,x_List,g_List]:=Take[NestList[TM2DStep[{s_,c_}:>With[{sp=s (2 c-1) I},{sp,1-c,{Re[sp],Im[sp]}}],#]&,{1,Table[0,{size[[1]]},{size[[2]]}],x},Last[g]],g+1]

LangtonsAnt[size_Integer,x_List,its_List]:=LangtonsAnt[{size,size},x,its]

LangtonsAnt[size_,x_List,it_Integer]:=LangtonsAnt[size,x,{it,it}]

LangtonsAntPlot[l_List,opts___]:=Module[
{
ant=Ant/.{opts}/.Options[LangtonsAnt]
},
ArrayPlot[#[[2]],
Sequence@@DeleteCases[{opts},Ant->_],
Mesh->True,
AspectRatio->Automatic,
Frame->False,
DataReversed->True,
Epilog->If[ant,
Ant[Reverse[#[[3]]]+{-1,-1}.5,#[[1]]],
{}
]
]&/@l
]

Options[LangtonsAnt]={Ant->True};

ASCII Representation

TableForm[l=NestList[TM2DStep[{s_,c_}:>With[{sp=s (2 c-1) I},{sp,1-c,{Re[sp],Im[sp]}}],#]&,{1,Table[0,{7},{7}],{4,4}},3],TableSpacing->{1,1}]

Animation Frames

V5

Show[GraphicsArray[Partition[LangtonsAntPlot[LangtonsAnt[{4,5},{3,3},{0,5}],
DisplayFunction->Identity
],3]
]]

V6

GraphicsGrid[Partition[LangtonsAntPlot[LangtonsAnt[{4,5},{3,3},{0,5}]],3]]

Animation

Export[gifs<>"langant.gif",Graphics/@Show/@LangtonsAntPlot[LangtonsAnt[{8,7},{6,3},{0,50}]],ConversionOptions->{"AnimationDisplayTime"->.25}
]//Timing

Time Steps

V5

Show[LangtonsAntPlot[LangtonsAnt[15,{8,8},386],MeshStyle->{GrayLevel[.7]},Ant->False]]//Timing

Show[LangtonsAntPlot[LangtonsAnt[{47,63},{24,31},10647],Ant->False,MeshStyle->{GrayLevel[.7]}]]//Timing

V6

Show[LangtonsAntPlot[LangtonsAnt[15,{8,8},386],MeshStyle->{GrayLevel[.7]},Ant->False]]

Show[LangtonsAntPlot[LangtonsAnt[{47,63},{24,31},10647],Ant->False,Mesh->False]]