One-dimensional cellular automata: Difference between revisions

(→‎{{header|Python}}: Section the two existing answers.)
(→‎Python: Sum neighbours == 2: New example added.)
Line 2,399:
a |= endvals
a = ((a&((a<<1) | (a>>1))) ^ ((a<<1)&(a>>1))) & endmask</lang>
 
===Python: Sum neighbours == 2===
This example makes use of the observation that a cell is alive in the next generation if the sum with its current neighbours of alive cells is two.
<lang python>>>> gen = [int(ch) for ch in '_###_##_#_#_#_#__#__'.replace('_', '0').replace('#', '1')]
>>> for n in range(10):
print(''.join('#' if cell else '_' for cell in gen))
gen = [0] + gen + [0]
gen = [sum(gen[m:m+3]) == 2 for m in range(len(gen)-2)]
 
_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________
__##________________
>>>
 
=={{header|R}}==
Anonymous user