-
Notifications
You must be signed in to change notification settings - Fork 0
/
cellAuto.py
142 lines (123 loc) · 2.22 KB
/
cellAuto.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import math
def cellular_automaton(seed, rule, gen):
runs = []
for i in range(0,gen):
seed = onegen(seed,rule)
print seed
runs.append(seed)
return runs
def onegen(seed, rule):
c = ""
for e in sectiongrab(seed):
if e == '...':
if (rule & 1):
c += 'x'
else:
c += '.'
if e == '..x':
if rule & 2:
c += 'x'
else:
c += '.'
if e == '.x.':
if rule & 4:
c += 'x'
else:
c += '.'
if e == '.xx':
if rule & 8:
c += 'x'
else:
c += '.'
if e == 'x..':
if rule & 16:
c += 'x'
else:
c += '.'
if e == 'x.x':
if rule & 32:
c += 'x'
else:
c += '.'
if e == 'xx.':
if rule & 64:
c += 'x'
else:
c += '.'
if e == 'xxx':
if rule & 128:
c += 'x'
else:
c += '.'
return c
def sectiongrab(s):
out = []
chunk = ""
for e in range(0,len(s)):
chunk = ""
for i in range(e,e+3):
#print i
if e + 3 > len(s):
chunk += s[i - len(s)]
#print chunk
else:
#print chunk
#print s[i]
chunk += s[i]
out.append(chunk)
return out
def repfinder(r):
reps = {}
for i in range(0,len(r)):
for j in range(0,len(r)):
if i == j:
continue
if r[i] == r[j]:
try:
if j in reps[r[i]]:
continue
reps[r[i]].append(j)
except:
reps[r[i]] = [j]
return reps
def classify(reps):
notconstpatt = []
for e in reps:
temp = reps[e][1]
tdiff = reps[e][1] - reps[e][0]
diff = 0
for ei in range(2,len(reps[e])):
diff = temp - reps[e][ei]
if math.fabs(tdiff) == math.fabs(diff):
pass
else:
notconstpatt.append(e)
tdiff = diff
temp = reps[e][ei]
return notconstpatt
def lotofrules(j):
smallest= 100
thing = ''
for i in range(0,j):
some = repfinder(cellular_automaton('x.xxx.x.',i,100))
#print "%s repitions" % len(some)
for e in some:
some[e].sort()
for e in some:
if len(some[e]) < smallest:
smallest = len(some[e])
thing = i
#print some
#return
return smallest, thing
# it= []
# it = classify(some)
# if it:
# return it
#print '\n\n' ,lotofrules(256)
some = repfinder(cellular_automaton('xxx.....',9,100))
print "%s repitions" % len(some)
for e in some:
some[e].sort()
print some
#print classify(some)