Elementary cellular automaton: Difference between revisions

(Go solution)
Line 198:
| ## # ### # |
| ## #### ## # ### |</pre>
 
=={{header|Python}}==
<lang python>def eca(cells, rule):
lencells = len(cells)
c = "0" + cells + "0" # Zero pad the ends
rulebits = '{0:08b}'.format(rule)
neighbours2next = {'{0:03b}'.format(n):rulebits[::-1][n] for n in range(8)}
yield c[1:-1]
while True:
c = ''.join(['0',
''.join(neighbours2next[c[i-1:i+2]]
for i in range(1,lencells+1)),
'0'])
yield c[1:-1]
 
for rule in (90, 30, 122):
print('\nRule: %i' % rule)
for i, c in zip(range(16), eca('0000000001000000000', rule)):
print('%2i: %s' % (i, c.replace('0', '.').replace('1', '#')))</lang>
 
{{out}}
<pre>Rule: 90
0: .........#.........
1: ........#.#........
2: .......#...#.......
3: ......#.#.#.#......
4: .....#.......#.....
5: ....#.#.....#.#....
6: ...#...#...#...#...
7: ..#.#.#.#.#.#.#.#..
8: .#...............#.
9: #.#.............#.#
10: ...#...........#...
11: ..#.#.........#.#..
12: .#...#.......#...#.
13: #.#.#.#.....#.#.#.#
14: .......#...#.......
15: ......#.#.#.#......
 
Rule: 30
0: .........#.........
1: ........###........
2: .......##..#.......
3: ......##.####......
4: .....##..#...#.....
5: ....##.####.###....
6: ...##..#....#..#...
7: ..##.####..######..
8: .##..#...###.....#.
9: ##.####.##..#...###
10: #..#....#.####.##..
11: #####..##.#....#.#.
12: #....###..##..##.##
13: ##..##..###.###..#.
14: #.###.###...#..####
15: #.#...#..#.#####...
 
Rule: 122
0: .........#.........
1: ........#.#........
2: .......#.#.#.......
3: ......#.#.#.#......
4: .....#.#.#.#.#.....
5: ....#.#.#.#.#.#....
6: ...#.#.#.#.#.#.#...
7: ..#.#.#.#.#.#.#.#..
8: .#.#.#.#.#.#.#.#.#.
9: #.#.#.#.#.#.#.#.#.#
10: .#.#.#.#.#.#.#.#.#.
11: #.#.#.#.#.#.#.#.#.#
12: .#.#.#.#.#.#.#.#.#.
13: #.#.#.#.#.#.#.#.#.#
14: .#.#.#.#.#.#.#.#.#.
15: #.#.#.#.#.#.#.#.#.#</pre>
Anonymous user