-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyze-chains.py
More file actions
executable file
·451 lines (355 loc) · 12.6 KB
/
Copy pathanalyze-chains.py
File metadata and controls
executable file
·451 lines (355 loc) · 12.6 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env python3
import os
import re
import sys
from glob import glob
from common import TARGETS, ascii_digits
CHAIN_PATTERN = re.compile(r"chain.*:\n( *x.*\n)+", re.MULTILINE)
FUNCTION_PATTERN = re.compile(r"[0-1]{10,16}")
def parse_chain(chain):
return FUNCTION_PATTERN.findall(chain)
def find_chains(data):
chains = []
for match in CHAIN_PATTERN.finditer(data):
chains.append(match.group(0))
return chains
def apply_operation(op, g, h):
if op == "&":
return g & h
if op == "|":
return g | h
if op == "^":
return g ^ h
if op == "<":
return (~g) & h
if op == ">":
return g & (~h)
def extract_possible_chains(
chain,
so_far=None,
remaining=None,
expressions=None,
expressions_index=None,
seen=None,
):
result = []
if not seen:
seen = set()
if so_far is None:
chain = [int(x, 2) for x in chain]
so_far = [
(None, None, None, chain[0], int("0000000011111111", 2)),
(None, None, None, chain[1], int("0000111100001111", 2)),
(None, None, None, chain[2], int("0011001100110011", 2)),
(None, None, None, chain[3], int("0101010101010101", 2)),
]
if remaining is None:
remaining = {chain[i] for i in range(4, len(chain))}
if not remaining:
return [so_far]
if not expressions:
expressions = []
expressions_index = 0
for k in range(1, len(so_far)):
h = so_far[k][3]
full_h = so_far[k][4]
for j in range(0, k):
g = so_far[j][3]
full_g = so_far[j][4]
f = g & h
full_f = full_g & full_h
if f in remaining and full_f not in seen:
expressions.append((j, "&", k, f, full_f))
f = g | h
full_f = full_g | full_h
if f in remaining and full_f not in seen:
expressions.append((j, "|", k, f, full_f))
f = g ^ h
full_f = full_g ^ full_h
if f in remaining and full_f not in seen:
expressions.append((j, "^", k, f, full_f))
f = (~g) & h
full_f = (~full_g) & full_h
if f in remaining and full_f not in seen:
expressions.append((j, "<", k, f, full_f))
f = g & (~h)
full_f = full_g & (~full_h)
if f in remaining and full_f not in seen:
expressions.append((j, ">", k, f, full_f))
else:
expressions = expressions[:]
k = len(so_far) - 1
h = so_far[k][3]
full_h = so_far[k][4]
for j in range(0, k):
g = so_far[j][3]
full_g = so_far[j][4]
f = g & h
full_f = full_g & full_h
if f in remaining and full_f not in seen:
expressions.append((j, "&", k, f, full_f))
f = g | h
full_f = full_g | full_h
if f in remaining and full_f not in seen:
expressions.append((j, "|", k, f, full_f))
f = g ^ h
full_f = full_g ^ full_h
if f in remaining and full_f not in seen:
expressions.append((j, "^", k, f, full_f))
f = (~g) & h
full_f = (~full_g) & full_h
if f in remaining and full_f not in seen:
expressions.append((j, "<", k, f, full_f))
f = g & (~h)
full_f = full_g & (~full_h)
if f in remaining and full_f not in seen:
expressions.append((j, ">", k, f, full_f))
for i in range(expressions_index, len(expressions)):
j, op, k, f, full_f = expressions[i]
if f not in remaining:
continue
next_remaining = remaining - {f}
next_seen = seen | {full_f}
result += extract_possible_chains(
chain,
so_far=so_far + [expressions[i]],
remaining=next_remaining,
expressions=expressions,
expressions_index=i + 1,
seen=next_seen,
)
return result
def extracted_chain_to_steps(extracted_chain):
steps = []
for i, (j, op, k, f, full_f) in enumerate(extracted_chain):
s = f"x{i + 1}"
if op:
s += f" = x{j + 1} {op} x{k + 1}"
s += f" = {f}"
if len(f) != len(full_f):
s += f" [{full_f}]"
for [target_bits, target] in TARGETS:
if target_bits.startswith(f):
s += f" = {target}"
break
steps.append(s)
return steps
def stats():
data = []
for fn in sys.argv[1:]:
with open(fn) as f:
data.append(f.read())
data = "\n".join(data)
chains = find_chains(data)
chain_sets = [set(parse_chain(chain)) for chain in chains]
print(len(chain_sets), "chains")
chain_set_map = {}
for s in chain_sets:
fs = frozenset(s)
chain_set_map[fs] = chain_set_map.get(fs, 0) + 1
print(len(chain_set_map), "unique chains")
by_size = {}
for fs in chain_set_map.keys():
by_size[len(fs)] = by_size.get(len(fs), 0) + 1
min_size = 1000
print("Chain sizes:")
for size, count in by_size.items():
print(size, ": ", count)
min_size = min(min_size, size)
os.makedirs("generated", exist_ok=True)
uniquely_generated = set()
for i, chain in enumerate(chains):
parsed_chain = parse_chain(chain)
fs = frozenset(parsed_chain)
if fs in uniquely_generated:
continue
uniquely_generated.add(fs)
with open(f"generated-{len(parsed_chain)}-{i}.txt", "w") as f:
f.write(chain)
def is_target(function):
for [target_bits, _target] in TARGETS:
if target_bits.startswith(function):
return True
return False
def extract_chains():
data = []
for fn in sys.argv[1:]:
with open(fn) as f:
data.append(f.read())
data = "\n".join(data)
chains = find_chains(data)
parsed_chains = [parse_chain(chain) for chain in chains]
min_size = min(len(x) for x in parsed_chains)
num_bits = len(parsed_chains[0][0])
parsed_chains = [x for x in parsed_chains if len(x) == min_size]
raw_extracted_chains = []
for parsed_chain in parsed_chains:
raw_extracted_chains += extract_possible_chains(parsed_chain)
extracted_chains = []
for chain in raw_extracted_chains:
new_chain = []
for j, op, k, f, full_f in chain:
new_chain.append(
(
j,
op,
k,
bin(f)[2:].rjust(num_bits, "0"),
bin(full_f)[2:].rjust(16, "0"),
)
)
extracted_chains.append(new_chain)
grouped_by_full_output_sets = {}
for extracted_chain in extracted_chains:
full_output = tuple(
sorted(frozenset(x[4] for x in extracted_chain if is_target(x[3])))
)
if full_output not in grouped_by_full_output_sets:
grouped_by_full_output_sets[full_output] = {}
unique_operations_set = frozenset(
extracted_chain[x[0]][4] + x[1] + extracted_chain[x[2]][4]
for x in extracted_chain
if x[1]
)
if (
unique_operations_set not in grouped_by_full_output_sets[full_output]
or extracted_chain
< grouped_by_full_output_sets[full_output][unique_operations_set]
):
grouped_by_full_output_sets[full_output][unique_operations_set] = (
extracted_chain
)
N = len(parsed_chains[0][0])
filename = f"publish/release/chains-{N}-{min_size}.txt"
print(f"writing to {filename}")
with open(filename, "w") as f:
for group_id, key in enumerate(sorted(grouped_by_full_output_sets.keys())):
unique_chains = grouped_by_full_output_sets[key].values()
f.write(f"Group {group_id} ({len(unique_chains)} unique chains):\n\n")
for candidate in sorted(unique_chains):
chain_steps = extracted_chain_to_steps(candidate)
f.write("\n".join(chain_steps))
f.write("\n\n")
digits = ascii_digits(chain_steps)
for i in range(16):
d = digits[i][0]
f.write(f"{d: 3d} ")
f.write("\n")
for i in range(16):
s1 = digits
s1 = digits[i][1]
f.write(f"{s1.rjust(4)}")
f.write("\n")
for i in range(16):
s2 = digits
s2 = digits[i][2]
f.write(f"{s2.rjust(4)}")
f.write("\n")
for i in range(16):
s3 = digits
s3 = digits[i][3]
f.write(f"{s3.rjust(4)}")
f.write("\n")
f.write("\n------\n\n")
def find_shortest_chain(chain, target):
involved_steps = set()
next_steps = []
for i, step in enumerate(chain):
if target.startswith(step[-2]):
next_steps.append(i)
break
while next_steps:
# print(target, next_steps, involved_steps)
index = next_steps.pop(0)
(left, _, right, *_) = chain[index]
involved_steps.add(index)
if (
left is not None
and left >= 4
and left not in involved_steps
and left not in next_steps
):
next_steps.append(left)
if (
right is not None
and right >= 4
and right not in involved_steps
and right not in next_steps
):
next_steps.append(right)
return involved_steps
def find_single_target_chain_lengths():
data = []
for fn in sys.argv[1:]:
with open(fn) as f:
data.append(f.read())
data = "\n".join(data)
chains = find_chains(data)
parsed_chains = [parse_chain(chain) for chain in chains]
min_size = min(len(x) for x in parsed_chains)
num_bits = len(parsed_chains[0][0])
parsed_chains = [x for x in parsed_chains if len(x) == min_size]
raw_extracted_chains = []
for parsed_chain in parsed_chains:
raw_extracted_chains += extract_possible_chains(parsed_chain)
extracted_chains = []
for chain in raw_extracted_chains:
new_chain = []
for j, op, k, f, full_f in chain:
new_chain.append(
(
j,
op,
k,
bin(f)[2:].rjust(num_bits, "0"),
bin(full_f)[2:].rjust(16, "0"),
)
)
extracted_chains.append(new_chain)
for extracted_chain in extracted_chains:
for target in TARGETS:
target_chain = find_shortest_chain(extracted_chain, target[0])
print(target, len(target_chain))
def get_smaller_chains():
chain_sets = {}
for fn in glob("output-partial-*-*.txt"):
# parse out the two integers from the filename
match = re.match(r"output-partial-(\d+)-(\d+).txt", fn)
t1, t2 = match.groups()
with open(fn) as f:
chains = find_chains(f.read())
chain_sets[frozenset((t1, t2))] = {
frozenset(parse_chain(chain)) for chain in chains
}
for key, chains in sorted(chain_sets.items()):
print(key, "has", len(chains), "chains")
return chain_sets
def solve(chain_sets, current_targets, current_set, index):
# print(index, len(current_targets), len(current_set))
if len(current_set) > 23:
return
if len(current_targets) == 7:
print("Found a solution!", len(current_set), current_set)
return
if len(current_targets) >= 4 and len(current_set) <= 17:
print("Promising...", len(current_set), current_set)
return
for i in range(index, len(chain_sets)):
if index == 0:
print(i, "/", len(chain_sets))
new_targets, new_set = chain_sets[i]
combined = current_targets | new_targets
if combined == current_targets:
continue
combined_set = current_set | new_set
solve(chain_sets, combined, combined_set, i + 1)
def try_puzzle():
chain_sets = []
for key, value in get_smaller_chains().items():
for v in value:
chain_sets.append((key, v))
solve(chain_sets, set(), set(), 0)
# try_puzzle()
# stats()
# extract_chains()
find_single_target_chain_lengths()