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.


grid_size=100;                              //side length of the square grid
ant_pos=round([grid_size/2 grid_size/2]);   //ant's initial position at center of grid
head_direction='W';                         //ant's initial direction can be either
                                            //'N' north, 'S' south, 'E' east, or 'W' west
 
grid=~zeros(grid_size,grid_size)    //blank grid
col=[];                             //cell color handler
next_step=%T;                       //step flag
i=0;                                //step counter
 
while next_step
 
    col=grid(ant_pos(1),ant_pos(2));    //get cell color
 
    if col then                         //if white cell
        grid(ant_pos(1),ant_pos(2))=~grid(ant_pos(1),ant_pos(2));  //switch color
        if head_direction=='N' then     //if head to N
            head_direction='E';         //turn right to E
            ant_pos(2)=ant_pos(2)+1;    //step forward
 
        elseif head_direction=='E' then //if head to E
            head_direction='S';         //turn right to S
            ant_pos(1)=ant_pos(1)+1;    //step forward
 
        elseif head_direction=='S' then //if head to S
            head_direction='W';         //turn right to W
            ant_pos(2)=ant_pos(2)-1;    //step forward
 
        elseif head_direction=='W' then //if head to W
            head_direction='N';         //turn right to N
            ant_pos(1)=ant_pos(1)-1;    //step forward
        end
    else                                //if black cell
        grid(ant_pos(1),ant_pos(2))=~grid(ant_pos(1),ant_pos(2));  //switch color
        if head_direction=='N' then     //if head to N
            head_direction='W';         //turn left to E
            ant_pos(2)=ant_pos(2)-1;    //step foward
 
        elseif head_direction=='W' then //if head to W
           head_direction='S';          //turn left to S
            ant_pos(1)=ant_pos(1)+1;    //step forward
 
        elseif head_direction=='S' then //if head to S
            head_direction='E';         //turn left to E
            ant_pos(2)=ant_pos(2)+1;    //step forward
 
        elseif head_direction=='E' then //if head to E
            head_direction='N';         //turn left to N
            ant_pos(1)=ant_pos(1)-1;    //step forward
        end
    end
 
    i=i+1;
 
    if ant_pos(1)<1 | ant_pos(1)>100 | ant_pos(2)<0 | ant_pos(2)>100 then   //check ant's position
        disp("Out of bounds after "+string(i)+" steps");
        next_step=~next_step;                                               //break loop if out of bounds
    end
end
 
ascii_grid=string(zeros(grid));     //create grid of chars to display
                                    //on the console
for a=1:length(grid)
    if grid(a) then
        ascii_grid(a)=" ";          //blank space if cell is white
    else
        ascii_grid(a)="#";          //# if cell is black
    end
end
 
disp(ascii_grid);