simulation.py
Wolfram Rule 30
This simulation iterates the elementary cellular automaton known as Rule 30. Starting from a single active cell, each step applies the bitwise rule. The final grid contains one row for each step so the entire evolution can be viewed at once.
from tys import progress, frame, probe
Run the Rule 30 simulation.
def simulate(cfg: dict):
width = cfg["width"]
steps = cfg["steps"]
state = 1 << (width // 2)
mask = (1 << width) - 1
def row_from_state(s: int) -> list[int]:
return [1 if (s >> (width - 1 - i)) & 1 else 0 for i in range(width)]
rows: list[list[int]] = []
for step in range(steps):
row = row_from_state(state)
rows.append(row)
probe("ones", step, sum(row))
progress(int(100 * (step + 1) / steps))
state = ((state >> 1) ^ (state | (state << 1))) & mask
row = row_from_state(state)
rows.append(row)
frame(0, rows)
ones = sum(row)
return {"ones": ones}
def requirements():
return {
"builtin": ["micropip", "pyyaml"],
"external": []
}
Default.yaml
width: 128
steps: 128
Charts (Default)
Final Frame (Default)
Final Results (Default)
Metric | Value |
---|---|
ones | 68.00 |