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.


include std\console.e
include std\graphics.e
 
sequence grid = repeat(repeat(1,100),100) --fill 100 by 100 grid with white (1)
sequence antData = {48, 53, 360} --ant x coordinate, y coordinate, facing angle
integer iterations = 0
 
--while ant isn't out of bounds of the 100 by 100 area..
while antData[1] > 0 and antData[1] < 100 and antData[2] > 0 and antData[2] < 100 do
    switch grid[antData[1]][antData[2]] do
        case 1 then--cell is already white
            grid[antData[1]][antData[2]] = 0 --cell turns black, ant turns right
            antData[3] += 90
            break
        case 0 then--cell is already black
            grid[antData[1]][antData[2]] = 1 --cell turns white, ant turns left
            antData[3] -= 90
            break
    end switch
    --wrap ant directions if > 360 or < 90 (by 90)
    switch antData[3] do
        case 450 then
            antData[3] = 90
            break
        case 0 then
            antData[3] = 360
            break
    end switch  
    --move ant based on its new facing, one square
    --first north, then south, east, west
    switch antData[3] do
        case 360 then
            antData[2] -= 1
            break
        case 180 then
            antData[2] += 1
            break
        case 90 then
            antData[1] += 1
            break
        case 270 then
            antData[1] -= 1
            break
    end switch
iterations += 1
end while
 
wrap(0) --don't wrap text output, the grid wouldnt display as a square
 
for y=1 to 100 do
    printf(1,"\n")
    for x=1 to 100 do
        switch grid[x][y] do--each grid block , based on color
            case 0 then
                printf(1,".")
                break
            case 1 then
                printf(1,"#")
                break
        end switch
    end for
end for     
 
printf(1,"\n%d Iterations\n",iterations)
any_key()--wait for keypress, put default message 'press any key..'