-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECA.py
67 lines (58 loc) · 1.63 KB
/
ECA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from optparse import OptionParser
#options
parser = OptionParser()
parser.add_option('-w', '--width',
dest='width', default=71,
help='The width of the diagram. Must be an odd number.')
parser.add_option('-r', '--rulenumber',
dest='rulenumber', default=90,
help='The Wolfram rule number.')
parser.add_option('-g', '--generations',
dest='gens', default=100,
help='The number of generations to run for.')
(options, args) = parser.parse_args()
#setting options variables
width = int(options.width)
rulenumber = int(options.rulenumber)
gens = int(options.gens)
"""width = 101
rulenumber = 90
gens = 100"""
# So can be opened as PBM file in GIMP
print 'P1 '+str(width)+' '+str(gens)
#the initial row
if width%2 == 0:
print "Width must be an odd number!!"
first_row = ((width - 1) / 2)*'0' + '1' + ((width - 1) / 2)*'0'
print first_row
def take_rule(int):
binary = []
for i in xrange(7,-1,-1):
binary.append((int & 2**i) >> i)
rule = { '111': binary[0], '110': binary[1], '101': binary[2], '100': binary[3], '011': binary[4], '010': binary[5], '001': binary[6], '000': binary[7] }
#print rule
return rule
def apply_rule(previous, rule):
for g in range(gens):
if g == 0:
previous = first_row
else:
previous = new
#print previous
new = ''
for c in range(width):
if previous[c-1]:
seq1 = previous[c-1]
else:
seq1 = previous[width-1]
seq2 = previous[c]
if c == width-1:
seq3 = previous[0]
else:
seq3 = previous[c+1]
seq = str(seq1) + str(seq2) + str(seq3)
#print seq
new += str(rule[seq])
print new
rule = take_rule(rulenumber)
apply_rule(first_row, rule)