Start free

Expert · Simulation · 259 of 264

Rule 30

A row of cells evolves by Rule 30: a cell's next state depends on itself and its two neighbours (cells beyond the ends count as dead). The patterns ###, ##., #.# and ... give dead; #.., .##, .#. and ..# give alive (#).

Input: the starting row, then the number of steps. Output: the row after each step (not the starting row).

Examples

Input
...#...
3
Output
..###..
.##..#.
##.####
Input
#
2
Output
#
#

One more test checks other inputs when you press Check.

Stuck? The idea behind simulation

Follow the rules step by step, exactly as written: keep the state (a grid, a position), and work out the next state from the current one.

When every cell changes at once, build the new grid apart from the old one.

x, y = 0, 0
for move in "NNEES":
    x += {"E": 1, "W": -1}.get(move, 0)
    y += {"N": 1, "S": -1}.get(move, 0)
print(x, y)

Watch for: Changing the grid while still reading it mixes this step with the next.

All the simulation exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More simulation exercises