Langton’s ant: two rules and a road out of chaos
The ant knows only the colour beneath it. It turns, flips that cell and moves on. Its trail first looks tangled, then a diagonal road emerges on an initially white plane. Where does the order come from when there is no instruction to build a road?
Video walkthrough
Two rules, one order of operations
On a white cell, turn 90° right. On a black cell, turn 90° left. Then flip that same cell and move one cell forward in the new direction. Read the colour before flipping it; the destination cell is not changed yet.
The ant has a position and a heading; it is not a third cell colour. An orange arrow represents it in the player. The cell beneath the arrow remains either white or black.
The first four moves
Start at (0, 0), facing north, with every cell white. X increases rightwards; Y increases downwards. First turn east, flip (0, 0) black and move to (1, 0). Then turn south to (1, 1), west to (0, 1), and north back to (0, 0). Four cells are now black.
Move five is different: the current cell is black. Turn left from north to west, flip (0, 0) white and move to (−1, 0). Use Next to inspect these moves and compare the result with the prediction below the board.
The field is the ant’s memory
There are no random choices. The same initial field and heading produce the same path. But the ant changes its environment and reads the effects of previous visits when it returns. Position and heading alone cannot determine the next move without the cell colours.
Try the black starting cell example: the very first turn is now left. Edit initial cells by clicking at step 0 or entering coordinates. Each edit starts a new history.
What repeats on the highway?
Starting on an all-white plane, the ant develops a steady diagonal motion after roughly ten thousand moves. Highway opens step 11,000 of this run. Reduce the speed, select Around the ant and compare states 104 moves apart: the heading repeats while the position shifts by two cells along each axis.
A moving pattern repeats, not the entire field: the trail remains behind. This is a specific run from a white plane. Observing it does not establish the same behaviour for every initial drawing.
The screen edge is not a boundary
The model uses an unbounded integer grid. Only black coordinates are stored; every missing cell is white. There are no walls, connected edges or wrapping. Overview fits the visited region and initial drawing; Around the ant shows a small area. Changing the camera does not change the computation.
This example records at most 16,000 moves. The editor accepts up to 256 initial black cells with coordinates from −32 to 32. These limit the input and recording length. The ant can travel beyond the editor’s coordinate range during a run.
How it differs from the Game of Life
In the Game of Life, every cell determines its next state simultaneously from its neighbours. Here, one ant changes one cell at a time. It does not count neighbours: it reads the current colour and uses its heading. Both models create complex patterns from local rules, but their update mechanisms differ.
Code for a single move
Headings are numbered 0, 1, 2, 3 clockwise. Adding 1 modulo 4 turns right; adding 3 turns left. The Set holds only black cells, using keys such as “x,y”. The function mutates the working Set and returns a new ant position; the player saves immutable history separately.
Show the executable model
const moves = [[0, -1], [1, 0], [0, 1], [-1, 0]];
function stepAnt(black, ant) {
const key = ant.x + "," + ant.y;
const wasBlack = black.has(key);
const direction = (ant.direction + (wasBlack ? 3 : 1)) % 4;
if (wasBlack) black.delete(key);
else black.add(key);
return {
x: ant.x + moves[direction][0],
y: ant.y + moves[direction][1],
direction
};
}
// 0 = north, 1 = east, 2 = south, 3 = west.
let ant = { x: 0, y: 0, direction: 0 };
const black = new Set();
ant = stepAnt(black, ant);Computation and rewind memory
Each move performs a constant number of Set operations. Assuming the usual average O(1) hash-table access, N moves take expected O(N) time. The working field uses O(B) memory for B currently black cells, plus O(1) for the ant. Rendering costs are separate.
For seeking, the player stores N+1 positions, N cell changes and checkpoints every 128 moves. Restoring a board copies the preceding checkpoint and applies at most 127 changes. History memory is O(N + Σ Bᵢ), where Bᵢ are checkpoint sizes. With a fixed interval its worst case is O(N² + N·B₀), although this run’s snapshots are much smaller. This is the separate cost of arbitrary seeking.
Test a prediction
Predict the first five moves, then check them. Flip the starting cell and explain the first turn. Change the initial heading and look for a rotated trail. Compare highway steps 11,000 and 11,104. A share link stores the initial field, heading, history length, camera, position and speed; it opens paused.