-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgol.py
More file actions
209 lines (163 loc) · 8.8 KB
/
gol.py
File metadata and controls
209 lines (163 loc) · 8.8 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from bhv.visualization import Image
# from bhv.native import NativePackedBHV as BHV
from bhv.np import NumPyPacked64BHV as BHV
from time import sleep, time_ns
import io
from PIL import Image as PImage
init = [
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . . . 1 1 . . . . . . 1 1 . . . . . . . . . . . . 1 1 . . .",
". . . . . . . . . . . . 1 . . . 1 . . . . 1 1 . . . . . . . . . . . . 1 1 . . .",
". 1 1 . . . . . . . . 1 . . . . . 1 . . . 1 1 . . . . . . . . . . . . . . . . .",
". 1 1 . . . . . . . . 1 . . . 1 . 1 1 . . . . 1 . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . 1 . . . . . 1 . . . . . . . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . . 1 . . . 1 . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ."
]
step1 = [
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . 1 . . . . . . . 1 . 1 . . . . . . . . . . . 1 1 . . .",
". . . . . . . . . . . . 1 1 . . . . . . 1 . . 1 . . . . . . . . . . . 1 1 . . .",
". 1 1 . . . . . . . . 1 1 . . . . 1 1 . . 1 . 1 . . . . . . . . . . . . . . . .",
". 1 1 . . . . . . . 1 1 1 . . . . 1 1 . . . 1 . 1 . . . . . . . . . . . . . . .",
". . . . . . . . . . . 1 1 . . . . 1 1 . . . . . 1 . . . . . . . . . . . . . . .",
". . . . . . . . . . . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
]
step2 = [
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . . . . . . . . . . . . . .",
". . . . . . . . . . . . 1 1 . . . . . . . 1 . 1 1 . . . . . . . . . . 1 1 . . .",
". . . . . . . . . . . 1 . 1 . . . . . . 1 1 . 1 1 . . . . . . . . . . 1 1 . . .",
". 1 1 . . . . . . . 1 . . . . . . 1 1 1 . 1 . 1 1 . . . . . . . . . . . . . . .",
". 1 1 . . . . . . . 1 . . 1 . . 1 . . 1 . . 1 . 1 . . . . . . . . . . . . . . .",
". . . . . . . . . . 1 . . . . . . 1 1 . . . . 1 . . . . . . . . . . . . . . . .",
". . . . . . . . . . . 1 . 1 . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ."
]
step30 = [
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . . . 1 1 . . . . . . 1 1 . . . . . . . . . . . . 1 1 . . .",
". . . . . . . . . . . . 1 . . . 1 . . . . 1 1 . . . . . . . . . . . . 1 1 . . .",
". 1 1 . . . . . . . . 1 . . . . . 1 . . . 1 1 . . . . . . . . . . . . . . . . .",
". 1 1 . . . . . . . . 1 . . . 1 . 1 1 . . . . 1 . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . 1 . . . . . 1 . . . . . . . 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . . 1 . . . 1 . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . 1 1 . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . 1 1 . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .",
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ."
]
W = 64
H = 128
def viz_to_grid(v: list[str]) -> list[list[bool]]:
return [[c == '1' for c in r.split(' ')] for r in v]
def pad_grid(g: list[list[bool]]) -> list[list[bool]]:
h = len(g)
w = len(g[0])
return [r + [False]*(W - w) for r in g] + [[False]*W for _ in range(H - h)]
def chunk_grid(g: list[list[bool]], w, h, w0=0, h0=0) -> list[list[bool]]:
return [r[w0:w] for r in g[h0:h]]
def grid_to_viz(g: list[list[bool]]) -> list[str]:
return [" ".join('1' if b else '.' for b in r) for r in g]
def grid_to_hv(g: list[list[bool]]) -> BHV:
return BHV.from_bitstream(c for r in g for c in r)
def hv_to_grid(hv: BHV) -> list[list[bool]]:
bs = list(hv.bits())
assert len(bs) == H*W
return [bs[i:i+W] for i in range(0, W*H, W)]
def step(hv: BHV) -> BHV:
l = hv.roll_word_bits(-1)
r = hv.roll_word_bits(1)
b = hv.roll_words(-1)
t = hv.roll_words(1)
tl = l.roll_words(1)
bl = l.roll_words(-1)
tr = r.roll_words(1)
br = r.roll_words(-1)
nbs = [l, r, b, t, tl, bl, tr, br]
return hv.select(BHV.window(nbs, 2, 3), BHV.window(nbs, 3, 3))
def sanity_check():
print(len(init), len([c for c in init[0] if c != ' ']))
for r in grid_to_viz(pad_grid(viz_to_grid(step30))):
print(r)
step13_hv = grid_to_hv(pad_grid(viz_to_grid(step30)))
# print(step13_hv.active())
assert hv_to_grid(step13_hv) == pad_grid(viz_to_grid(step30))
print()
for r in grid_to_viz(hv_to_grid(step13_hv.roll_word_bits(1))):
print(r)
def run(initial_viz: list[str], generations: int):
initial = viz_to_grid(initial_viz)
h = len(initial)
w = len(initial[0])
petri_dish_hv = grid_to_hv(pad_grid(initial))
print(0)
for r in initial_viz:
print(r)
sleep(4)
for gen in range(1, generations):
print(gen)
petri_dish_hv = step(petri_dish_hv)
for r in grid_to_viz(chunk_grid(hv_to_grid(petri_dish_hv), w, h)):
print(r)
sleep(.4)
def export(initial_viz: list[str], generations: int, filename: str):
init_hv = grid_to_hv(pad_grid(viz_to_grid(initial_viz)))
petri_dish_history = [init_hv]
for _ in range(generations):
petri_dish_history.append(step(petri_dish_history[-1]))
# with open(f"{filename}.pbm", 'wb') as f:
# Image(petri_dish_history).pbm(f, binary=True)
t = Image(petri_dish_history).gif(binary=True, H=H, W=W)
frames = [PImage.open(io.BytesIO(frame)).convert('L') for frame in t]
frames[0].save(
f"{filename}.gif",
save_all=True,
append_images=frames[1:],
duration=100, # Adjust the frame duration as needed
loop=0
)
def benchmark():
init_hv = grid_to_hv(pad_grid(viz_to_grid(init)))
step30_hv = grid_to_hv(pad_grid(viz_to_grid(step30)))
petri_dish_hv = init_hv
for _ in range(30):
petri_dish_hv = step(petri_dish_hv)
print(petri_dish_hv == step30_hv)
t0 = time_ns()
for _ in range(1000000):
petri_dish_hv = step(petri_dish_hv)
t1 = time_ns()
print(1000000/((t1 - t0)/1e9))
# run(init, 50)
# benchmark()
export(init, 8191, "../bhv/cnative/gol8192.pbm")