The program below generates the first 32 generations of the cellular automaton governed by rule 30. The rule number is turned into its binary representation in the list rule
(in this case, [0,1,1,1,1,0,0,0]
- note that the least significant bit is stored first). This list can then be indexed for each cell by turning the states of the three parent cells into a number 0–7.
# The rule number and its binary representation as a list of 1s and 0s
# (least-significant bit first)
n = 30
rule = [n // 2**i % 2 for i in range(8)]
width = 80
ngens = 32
# The previous row: initialise with a single 1 in the centre
lrow = [0] * width
lrow[39] = 1
# An array representing the next generation
row = [0] * width
def line(row):
""" Outputs a string representation of the row of on/off cells. """
return ''.join(['*' if cell else ' ' for cell in row])
# Print the initial row
print(line(lrow))
# Evolve for ngens generations
for n in range(ngens):
for i in range(1,width-1):
# Apply the rule to cell i by turning the state of the three parent
# cells into an index to the rule list
row[i] = rule[4*lrow[i-1] + 2*lrow[i] + lrow[i+1]]
print(line(row))
# The present generation becomes the previous one before the next iteration
lrow = row[:]
The intriguing output is:
*
***
** *
** ****
** * *
** **** ***
** * * *
** **** ******
** * *** *
** **** ** * ***
** * * **** ** *
** **** ** * * ****
** * *** ** ** * *
** **** ** *** *** ** ***
** * * *** * *** * *
** **** ** * * ***** *******
** * *** **** * *** *
** **** ** *** ** ** * ***
** * * *** * ** *** **** ** *
** **** ** * ****** * * *** ****
** * *** **** **** *** ** * *
** **** ** *** * ** * * * *** ***
** * * *** * *** ** * *** ** * * * *
** **** ** * *** * * **** * * ** ******
** * *** **** ** ***** * ***** * * *
** **** ** *** * ** * * ** * ***** ***
** * * *** * ** * **** ** * ** ** * ** *
** **** ** * *** * * * *** **** * ** * ** * ****
** * *** **** **** ** ** *** * * **** * * *
** **** ** *** * ** * * *** * ** **** *** ** ***
** * * *** * ** * * ***** * ****** * * ** * * *
** **** ** * *** * * **** **** **** ** * * *********
** * *** **** **** * * ** * ** * * * * *