-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpast.py
145 lines (121 loc) · 3.06 KB
/
past.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
142
143
144
145
class Past(object):
data = [None for i in range(9)]
def __init__(self, data):
self.data = data % 512
def __str__(self):
return str(self.data)
def __repr__(self):
return str(self)
def __cmp__(self, other):
return cmp(self.data, other.data)
### Individual cells in the past. ###
@property
def nw(self):
return 1 if self.data & 256 else 0
@property
def n(self):
return 1 if self.data & 128 else 0
@property
def ne(self):
return 1 if self.data & 64 else 0
@property
def w(self):
return 1 if self.data & 32 else 0
@property
def c(self):
return 1 if self.data & 16 else 0
@property
def e(self):
return 1 if self.data & 8 else 0
@property
def sw(self):
return 1 if self.data & 4 else 0
@property
def s(self):
return 1 if self.data & 2 else 0
@property
def se(self):
return 1 if self.data & 1 else 0
### The resulting center cell after a single step. ###
@property
def z(self):
neighbors = sum([self.nw, self.n, self.ne,
self.w, self.e,
self.sw, self.s, self.se])
if self.c:
if neighbors < 2:
return 0
elif neighbors in [2, 3]:
return 1
else:
return 0
else:
if neighbors == 3:
return 1
else:
return 0
### Transformations for corroboration. ###
@property
def NW(self):
x = [self.nw, self.n,
self.w, self.c]
return vector_to_int(x)
@property
def N(self):
x = [self.nw, self.n, self.ne,
self.w, self.c, self.e]
return vector_to_int(x)
@property
def NE(self):
x = [self.n, self.ne,
self.c, self.e]
return vector_to_int(x)
@property
def W(self):
x = [self.nw, self.n,
self.w, self.c,
self.sw, self.s]
return vector_to_int(x)
@property
def E(self):
x = [self.n, self.ne,
self.c, self.e,
self.s, self.se]
return vector_to_int(x)
@property
def SW(self):
x = [self.w, self.c,
self.sw, self.s]
return vector_to_int(x)
@property
def S(self):
x = [self.w, self.c, self.e,
self.sw, self.s, self.se]
return vector_to_int(x)
@property
def SE(self):
x = [self.c, self.e,
self.s, self.se]
return vector_to_int(x)
def vector_to_int(v):
r = 0
for i in v:
r <<= 1
r += i
return r
if __name__ == '__main__':
from random import randint
p = Past(randint(0, 256))
print p, '>', p.z
print p.nw, p.n, p.ne
print p.w, p.c, p.e
print p.sw, p.s, p.se
print
print 'NW:', p.NW
print 'N:', p.N
print 'NE:', p.NE
print 'W:', p.W
print 'E:', p.E
print 'SW:', p.SW
print 'S:', p.S
print 'SE:', p.SE