One rule, one row, and a step that never mixes time
Build the smallest possible cellular automaton: a periodic row, a rule that fires when exactly one neighbor is active, and a synchronous update readable as plain text.
A cellular automaton is four things: space, state, neighborhood, rule. This chapter takes the smallest version of that list. One row of 41 cells on a ring, each cell empty or active, and one local rule: a cell becomes active when exactly one of its three neighborhood cells is active.
The program below is that automaton and nothing else. It prints the world as text, one row per generation, so the space-time diagram can be read without a plotting library. Rows are time, columns are space, and the row wraps: a pattern leaving the right edge reappears on the left.
Run it
Save this as first_automaton.py and run it with Python 3.11 or later. It
needs NumPy only.
import numpy as np
width = 41
state = np.zeros(width, dtype=np.uint8)
state[width // 2] = 1
def local_rule(left, centre, right):
return int(left + centre + right == 1)
def step(state, rule):
next_state = np.zeros_like(state)
for i in range(len(state)):
left = state[(i - 1) % len(state)]
centre = state[i]
right = state[(i + 1) % len(state)]
next_state[i] = rule(left, centre, right)
return next_state
def render_row(state):
return "".join("#" if cell else "." for cell in state)
for _ in range(20):
print(render_row(state))
state = step(state, local_rule)The program is the chapter’s own code composed into one file: the initial
state, the local rule, the step engine, and the text renderer with its
twenty-generation print loop.
Expected outcome
....................#....................
...................###...................
..................#...#..................
.................###.###.................
................#.......#................
...............###.....###...............
..............#...#...#...#..............
.............###.###.###.###.............
............#...............#............
...........###.............###...........
..........#...#...........#...#..........
.........###.###.........###.###.........
........#.......#.......#.......#........
.......###.....###.....###.....###.......
......#...#...#...#...#...#...#...#......
.....###.###.###.###.###.###.###.###.....
....#...............................#....
...###.............................###...
..#...#...........................#...#..
.###.###.........................###.###.One # in the middle becomes a widening chevron whose interior fills with the
rule’s own structure. By generation 16 the pattern has reached both ends of
the 41-cell ring, and the two fronts are closing on each other. Nothing in
the code knows about a chevron, a width or a collision. Each cell only ever
sees three bits.
The rule is three bits, the engine is everything else
The rule is one function of three bits, and the chapter notes that this is not
an arbitrary choice: “exactly one active cell” counts the neighborhoods 100,
010 and 001, which are output bits 4, 2 and 1, so this rule is Rule 22
(16 + 4 + 2 = 22).
def local_rule(left, centre, right):
return int(left + centre + right == 1)Everything else is the engine: the neighborhood lookup, the time stepping and the boundary behavior. The engine never decides anything; it hands three bits to the rule and writes the answer into a separate array.
That separation is load-bearing. The chapter is explicit that updating the
current row in place is a bug, because cells later in the loop would read
already-updated neighbors and the loop would silently mix two time steps.
Writing to next_state makes every cell move from t to t + 1 together,
which is what makes the run reproducible.
The % len(state) in the lookup is the other half of the model. It is not an
implementation convenience; it declares the world to be a ring, and the
chapter’s boundary table makes the point that periodic boundaries remove the
edge while adding a finite-size artifact in its place.
Mechanism and limitations
Documented: the chapter states that the rule “becomes active when exactly one of the three cells in its neighborhood is active” and that this is Rule 22; that periodic boundaries come from the modulo indexing; that in-place updates mix two time steps; and that the boundary menu (periodic, fixed, reflective, infinite/sparse) is part of the model, not an implementation detail.
Observed: the run above. A single active cell produces a widening two-sided pattern that spans almost the whole 41-cell ring by generation sixteen. The output is a space-time diagram in text form, rows for time and columns for space, with no plotting library involved.
Not shown. The chapter’s own claims are not established here. Nothing classifies Rule 22 among the 256 elementary rules, and no property of the pattern (period, density, chaotic behavior) is measured. The boundary menu is described but only periodic is exercised: no fixed, reflective or infinite/sparse run is compared, so no claim is made about how the result would change under a different boundary. Finite-size effects on a 41-cell ring are visible in the last rows but not quantified, and the chapter’s warning that conclusions seen at one width should not be trusted is left untested.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.