Langton's ant is a cellular automaton that models an ant sitting on a plane of cells, all of which are white initially, the ant facing in one of four directions.

Each cell can either be black or white.

The ant moves according to the color of the cell it is currently sitting in, with the following rules:

   If the cell is black, it changes to white and the ant turns left;
   If the cell is white, it changes to black and the ant turns right;
   The ant then moves forward to the next cell, and repeat from step 1.

This rather simple ruleset leads to an initially chaotic movement pattern, and after about 10000 steps, a cycle appears where the ant moves steadily away from the starting location in a diagonal corridor about 10 cells wide.
Conceptually the ant can then walk infinitely far away.



Task

Start the ant near the center of a 100x100 field of cells, which is about big enough to contain the initial chaotic part of the movement.

Follow the movement rules for the ant, terminate when it moves out of the region, and show the cell colors it leaves behind.



The problem has received some analysis; for more details, please take a look at the Wikipedia article   (a link is below)..



See also

   Wikipedia:   Langton's ant.


Related task

   Rosetta Code:   Conway's Game of Life.


/*REXX program implements Langton's ant walk and displays the ant's path (finite field).*/
parse arg dir char seed .                        /*obtain optional arguments from the CL*/
if datatype(seed, 'W')   then call random ,,seed /*Integer? Then use it as a RANDOM SEED*/
if  dir=='' |  dir==","  then dir= random(1, 4)  /*ant is facing a random direction,    */
if char=='' | char==","  then char= '#'          /*binary colors:   0≡white,  1≡black.  */
parse value scrSize() with sd          sw .      /*obtain the terminal's depth and width*/
                           sd= sd -6;  sw= sw -1 /*adjust for     "      useble area.   */
   xHome= 1000000;      yHome= 1000000           /*initially in the middle of nowhere.  */
x= xHome;            y= yHome                    /*start ant's walk in middle of nowhere*/
$.= 1  ;  $.0= 4 ;   $.2= 2 ;   $.3= 3;   $.4= 4 /* 1≡north   2≡east   3≡south   4≡west.*/
minX= x;  minY= y;   maxX= x;   maxY= y          /*initialize the min/max values for X,Y*/
@.= 0                                            /*the universe  (walk field)  is white.*/
     do #=1  until (maxX-minY>sw)|(maxY-minY>sd) /*is the path out─of─bounds for screen?*/
     black= @.x.y;                 @.x.y= \@.x.y /*invert (flip)  ant's cell color code.*/
     if black  then dir= dir - 1                 /*if cell color was black,  turn  left.*/
               else dir= dir + 1                 /* "   "    "    "  white,  turn right.*/
     dir= $.dir                                  /*$ array handles/adjusts under & over.*/
               select                            /*ant walks the direction it's facing. */
               when dir==1  then y= y + 1        /*is ant walking north?  Then go up.   */
               when dir==2  then x= x + 1        /* "  "     "     east?    "  "  right.*/
               when dir==3  then y= y - 1        /* "  "     "    south?    "  "  down. */
               when dir==4  then x= x - 1        /* "  "     "     west?    "  "  left. */
               end   /*select*/                  /*the  DIRection  is always normalized.*/
     minX= min(minX, x);    maxX= max(maxX, x)   /*find the minimum and maximum of  X.  */
     minY= min(minY, y);    maxY= max(maxY, y)   /*  "   "     "     "     "     "  Y.  */
     end   /*steps*/                             /* [↑]  ant walks  hither and thither. */
                                                 /*finished walking, it's out-of-bounds.*/
say center(" Langton's ant walked "     #     ' steps ', sw, "─")
@.xHome.yHome= '█'                               /*show the ant's initial starting point*/
@.x.y=         '∙'                               /*show where the ant went out─of─bounds*/
                                                 /* [↓]  show Langton's ant's trail.    */
     do    y=maxY  to minY  by -1;  _=           /*display a single  row  of cells.     */
        do x=minX  to maxX;         _=_ || @.x.y /*build a cell row for the display.    */
        end   /*x*/                              /* [↓]  strip trailing blanks from line*/
     _= strip( translate(_, char, 10),  'T')     /*color the cells:   black  or  white. */
     if _\==''  then say _                       /*display line (strip trailing blanks).*/
     end      /*y*/                              /*stick a fork in it,  we're all done. */