-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.py
executable file
·65 lines (54 loc) · 1.46 KB
/
24.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
#!/usr/bin/env python3
import itertools as it
with open("data/24.txt") as f:
lines = [line.strip() for line in f.readlines()]
plen = 18
nump = len(lines) // plen
instructions = iter(lines)
programs = [list(it.islice(instructions, plen)) for _ in range(nump)]
# We only have a chance to reduce z if params[i][0] is 26;
# So we need to make sure that we can hit the correct value on those
params = [
(
int(p[4].split(" ")[-1]),
int(p[5].split(" ")[-1]),
int(p[15].split(" ")[-1]),
)
for p in programs
]
def p(w, z, a, b, c):
div = z // a
rest = z % 26
if rest + b == w:
return div
return div * 26 + w + c
def build_solution(digits, z, gen):
i = len(digits)
if i == 14 and z == 0:
return digits
a, b, c = params[i]
if a == 1:
for w in gen():
res = build_solution(
digits + str(w),
p(w, z, a, b, c),
gen,
)
if res is not None:
return res
else:
rest = z % 26
for w in gen():
if rest + b == w:
res = build_solution(
digits + str(w),
p(w, z, a, b, c),
gen,
)
if res is not None:
return res
return None
pt1 = build_solution("", 0, lambda: range(9, 0, -1))
print(pt1)
pt2 = build_solution("", 0, lambda: range(1, 10))
print(pt2)