Here is my implementation for one dimensional cellular automation in Python. Any suggestions for more efficiency - other strategies?
ncells = ['111','110','101','100','011','010','001','000']
def next_state (state, rule):
newstate = ''
binrule = bin(rule)[2:]
binrule = (8-len(binrule))*'0' + binrule
for i in range(len(state)):
cells = state[i-1] + state[i] + state[(i+1)%len(state)]
newstate += binrule[ncells.index(cells)]
return newstate
def wolfram (init_state, generations, rule):
state = init_state
for i in range(generations):
yield state
state = next_state(state, rule)
for i in wolfram('000000000010000000000', 30, 30):
print i