/
elbow0.py
430 lines (320 loc) · 12 KB
/
elbow0.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import golly as g
from time import time
from glife.text import make_text
#import cProfile, pstats
LANE1 = 0
LANE2 = 0
FULL_DEPTH = 4
CLEANUP_DEPTH = 1
MAX_POP = [40, 40, 40, 40, 30, 30, 30, 30]
GENS = 200
MAX_DIFF = 72
OUTFILE = '/home/user/life/elbow_0hd_%d.txt' % time()
def to_pairs(cells):
return zip(cells[::2], cells[1::2])
G_NE = g.parse('3o$2bo$bo!')
G_NW = g.parse('3o$o$bo!')
G_SW = g.transform(g.parse('bo$o$3o!'), 0, -2)
G_SE = g.transform(g.parse('bo$2bo$3o!'), -2, -2)
LWSS_W = g.transform(g.parse('bo2bo$o$o3bo$4o!'), 0, -1)
LWSS_S = g.transform(g.parse('bobo$o$o$o2bo$3o!'), -2, -4)
GLIDERS_SW = [to_pairs(g.evolve(G_SW, i)) for i in range(4)]
GLIDERS_SE = [to_pairs(g.evolve(G_SE, i)) for i in range(4)]
GLIDERS_NW = [to_pairs(g.evolve(G_NW, i)) for i in range(4)]
LWSSES_W = [to_pairs(g.evolve(LWSS_W, i)) for i in range(4)]
LWSSES_S = [to_pairs(g.evolve(LWSS_S, i)) for i in range(4)]
assert(all((0,0) in gl for gl in GLIDERS_SW))
assert(all((0,0) in gl for gl in GLIDERS_SE))
assert(all((0,0) in gl for gl in GLIDERS_NW))
assert(all((0,0) in lwss for lwss in LWSSES_W))
assert(all((0,0) in lwss for lwss in LWSSES_S))
def get_g0(lane):
x = lane // 2 - 5
glider = g.transform(G_NE, x, lane - x)
return g.evolve(glider, 2 * (1 + lane % 2))
G1 = get_g0(LANE1)
G2 = get_g0(LANE2)
b64 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$#"
def coord_to_string(x, y):
return b64[x & 63] + b64[y & 63] + b64[((x >> 6) & 7) + ((y >> 3) & 56)]
def canonical(cells):
return "".join(sorted(coord_to_string(x, y) for x, y in to_pairs(cells)))
block_elbows = [False, "D", "C", "B", "A", False, False,
False, False, "Ar", "Br", "Cr", "Dr"]
hive_elbows = ["E", "F", "G", "H", "I", "J", "K", False, "L", "M", False, False, "N"]
hf_elbows = ["Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P",
"Pr", "Qr", "Rr", "Sr", "Tr", "Ur", "Vr", "Wr", "Xr", "Yr", "Zr"]
HF_CELLS = sorted(to_pairs(g.parse("6bo$5bobo$5bobo$6bo2$b2o7b2o$o2bo5bo2bo$b2o7b2o2$6bo$5bobo$5bobo$6bo!")))
def is_elbow(cells):
# detect blocks
if len(cells) == 8:
# rule out tubs
if max(cells[::2]) - min(cells[::2]) != 1:
return False
# work out the elbow type based on the value of the furthest NW lane
min_l = min(cells[i] + cells[i+1] for i in range(0, len(cells), 2))
if abs(min_l) <= 6:
return block_elbows[min_l + 6]
elif len(cells) == 12:
# hives are the only pattern with 6 cells and 4x3 bounding box
w = max(cells[::2]) - min(cells[::2]) + 1
h = max(cells[1::2]) - min(cells[1::2]) + 1
if (w, h) == (4, 3):
reflect = ""
elif (w, h) == (3, 4):
reflect = "r"
else:
return False
min_l = min(cells[i] + cells[i+1] for i in range(0, len(cells), 2))
if reflect:
min_l = -min_l
if abs(min_l) <= 6:
elbow = hive_elbows[min_l + 6]
return elbow + reflect if elbow else False
elif len(cells) == 48:
x0 = min(cells[::2])
y0 = min(cells[1::2])
canon_cells = sorted([(x-x0, y-y0) for x, y in to_pairs(cells)])
if canon_cells != HF_CELLS:
return False
min_l = min(cells[i] + cells[i+1] for i in range(0, len(cells), 2))
return hf_elbows[min_l + 15] if -15 <= min_l <= 6 else False
return False
def test(cells, lane):
cells2 = g.evolve(cells, 4)
if len(cells) != len(cells2):
return 0, [], None
sumx1, sumy1 = sum(cells[::2]), sum(cells[1::2])
sumx2, sumy2 = sum(cells2[::2]), sum(cells2[1::2])
delta = (sumx2 - sumx1, sumy2 - sumy1)
for _ in range(4):
cells2 = g.evolve(cells2, 4)
sumx1, sumy1 = sumx2, sumy2
sumx2, sumy2 = sum(cells2[::2]), sum(cells2[1::2])
new_delta = (sumx2 - sumx1, sumy2 - sumy1)
if new_delta != delta:
return 0, [], None
# a,b,c,d,e are used to convert x, y values into a lane number and output type
if delta == (0, 0):
spaceships = []
# elif delta == (-5, 5):
# spaceships = GLIDERS_SW
# a, b, c, d, e = 1, 1, -1, 0, 0
elif delta == (5, 5):
spaceships = GLIDERS_SE
a, b, c, d, e = 1, -1, 1, 0, 1
elif delta == (-5, -5):
spaceships = GLIDERS_NW
a, b, c, d, e = 1, -1, 2, 0, 2
# elif delta == (-18, 0) or delta == (-24, 0):
# spaceships = LWSSES_W
# a, b, c, d, e = 0, 2, 0, 1, 3
# elif delta == (0, 18) or delta == (0, 24):
# spaceships = LWSSES_S
# a, b, c, d, e = 2, 0, 0, 1, 4
else:
return 0, [], None
pairs = to_pairs(cells)
if spaceships and lane is None:
found = False
for x0, y0 in pairs:
for phase, ss in enumerate(spaceships):
if all((x0+i, y0+j) in pairs for (i, j) in ss):
for i, j in ss:
pairs.remove((x0+i, y0+j))
found = True
lane = a * x0 + b * y0 + c + d * (x0+y0)%2, e, phase % 2
break
if found:
break
if not found:
return 0, [], None
cells = []
for x, y in pairs:
cells.append(x)
cells.append(y)
sort = sorted(pairs)
for p in range(2):
cells = g.evolve(cells, 1)
if sorted(to_pairs(cells)) == sort:
return p + 1, cells, lane
return 0, [], None
offset = 0
def show_it(recipe, lane, move, elbow_type, start_elbow):
global offset
start_cells, start_type, start_lane = start_elbow
res = ""
phase = 0
if lane is not None:
direction = lane[1]
phase = lane[2]
if direction == 0:
res = "Rev%d" % lane[0]
elif direction == 1:
res = "R%d" % (lane[0] - start_lane)
elif direction == 2:
res = "L%d" % (lane[0] - start_lane)
# elif direction == 3:
# res = "LWSS_W"
# elif direction == 4:
# res = "LWSS_S"
if move is None:
res += "k"
else:
res += "m%d%s%s" % (move - start_lane, str(start_type), elbow_type)
g.putcells(make_text(res, "mono"), offset, -80)
g.putcells(start_cells, offset, 0)
for i, t in enumerate(recipe[::2]):
if t is not None:
d = 80*i + MAX_DIFF / 4 + 20
g.putcells(g.evolve(G1, t + MAX_DIFF), offset-d, d)
for i, t in enumerate(recipe[1::2]):
if t is not None:
d = 80*i + MAX_DIFF / 4 + 20
g.putcells(g.evolve(G2, t + MAX_DIFF), offset-d, d)
res += ": "
for i in range(0, len(recipe), 2):
if recipe[i] is None:
res += "eo"[(recipe[i+1]+phase)%2] + "-9999 "
elif recipe[i+1] is None:
res += "eo"[(recipe[i]+phase)%2] + "9999 "
else:
res += "eo"[(recipe[i]+phase)%2] + str(recipe[i]-recipe[i+1]) + " "
f.write(res + "\n")
f.flush()
offset += 100
g.update()
def store(cells, lane, recipe, period, depth, next_pats):
old_depth = -1
# ignore parity of output glider when canonicalising.
# assumes we can change parity by delaying by one tick.
lane_str = "_None" if lane is None else "_%d_%d" % lane[:2]
canon = canonical(cells) + lane_str
if canon in depths:
old_depth = depths[canon]
elif period == 2:
canon1 = canonical(g.evolve(cells, 1)) + lane_str
if canon1 in depths:
old_depth = depths[canon1]
if old_depth < depth:
depths[canon] = depth
if depth > 0:
next_pats.append((cells, lane, recipe, period, depth))
return True
else:
return False
def get_patterns(cells, period):
if not cells:
return
# calculate where the gliders first hit the pattern
min_lane = 99999
for i in range(0, len(cells), 2):
if LANE1 - 3 <= cells[i] + cells[i+1] <= LANE2 + 6:
min_lane = min(min_lane, cells[i] - cells[i+1])
if min_lane == 99999:
return
minx = min_lane // 2
g1 = g.transform(G1, minx, -minx)
g2 = g.transform(G2, minx, -minx)
# Singletons
for t in range(period):
yield cells + g.evolve(g1, t), t, None
# Pairs
for phase in range(period):
tg2 = g.transform(g.evolve(g2, 2), -4, 4) #-14 gens
for t in range(15, MAX_DIFF + 1):
tg2 = g.transform(g.evolve(tg2, 3), -1, 1)
yield cells + g1 + tg2, phase, phase - t
g1 = g.evolve(g1, 1)
g2 = g.evolve(g2, 1)
def max_lane(cells):
return max(cells[i] - cells[i+1] for i in range(0, len(cells), 2))
def search(elbow):
global depths
start_elbow = elbow, is_elbow(elbow), max_lane(elbow)
#assume elbow is p1
new_pats = [(elbow, None, (), 1, FULL_DEPTH)]
depths = {}
start = True
iteration = 0
while new_pats:
iteration += 1
next_pats = []
n = 0
for cells, lane, recipe, period, depth in new_pats:
g.show(str((start_elbow[1], iteration, n, len(new_pats))))
n += 1
# only fire stuff at an elbow at the very beginning
if not start and is_elbow(cells):
continue
start = False
for start_cells, t1, t2 in get_patterns(cells, period):
end_cells = g.evolve(start_cells, GENS)
if len(end_cells) > 2 * (MAX_POP[iteration-1] + 12):
continue
new_period, end_cells, new_lane = test(end_cells, lane)
if new_period == 0:
continue
if len(end_cells) > 2 * MAX_POP[iteration-1]:
continue
new_depth = depth - 1
new_recipe = recipe + (t1, t2)
if lane is None and new_lane is not None:
new_depth += CLEANUP_DEPTH
if store(end_cells, new_lane, new_recipe, new_period, new_depth, next_pats):
# Elbow killing recipes
# if new_lane is not None and not end_cells:
# show_it(new_recipe, new_lane, None, startelbow)
elbow = is_elbow(end_cells)
if elbow:
move = max_lane(end_cells)
show_it(new_recipe, new_lane, move, elbow, start_elbow)
new_pats = next_pats
BLOCK = g.parse("2o$2o!")
HIVE = g.parse("b2o$o2bo$b2o!")
HF = g.parse("6bo$5bobo$5bobo$6bo2$b2o7b2o$o2bo5bo2bo$b2o7b2o2$6bo$5bobo$5bobo$6bo!")
ELBOWS = [g.transform(BLOCK, 0, -2),
g.transform(BLOCK, 0, -3),
g.transform(BLOCK, 0, -4),
g.transform(BLOCK, 0, -5),
g.transform(HIVE, 0, -7),
g.transform(HIVE, 0, -6),
g.transform(HIVE, 0, -5),
g.transform(HIVE, 0, -4),
g.transform(HIVE, 0, -3),
g.transform(HIVE, 0, -2),
g.transform(HIVE, 0, -1),
g.transform(HIVE, 1, 0),
g.transform(HIVE, 2, 0),
g.transform(HIVE, 5, 0),
g.transform(HF, 0, -11),
g.transform(HF, 0, -12),
g.transform(HF, 0, -13),
g.transform(HF, 0, -14),
g.transform(HF, 0, -15),
g.transform(HF, 0, -16),
g.transform(HF, 0, -17),
g.transform(HF, 0, -18),
g.transform(HF, 0, -19),
g.transform(HF, 0, -20),
g.transform(HF, 0, -21)]
# sanity check to make sure all elbows are distinct
elbow_types = set()
for elbow in ELBOWS:
elbow_type = is_elbow(elbow)
assert(elbow_type != False)
assert(elbow_type.isupper())
assert(elbow_type not in elbow_types)
elbow_types.add(elbow_type)
#prof = open("/home/user/life/elbow.stats", "w")
#pr = cProfile.Profile()
#pr.enable()
# do it
f = open(OUTFILE, 'w')
g.new('')
for elbow in ELBOWS:
search(elbow)
f.close()
#pr.disable()
#pstats.Stats(pr, stream=prof).sort_stats("cumulative").print_stats()
#prof.close()