-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_24.py
147 lines (123 loc) · 3.19 KB
/
day_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
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
import itertools
import aoc_helper
from aoc_helper import (
decode_text,
extract_ints,
frange,
irange,
iter,
list,
map,
range,
tail_call,
)
raw = aoc_helper.fetch(24, 2021)
def decode(reg):
if i := extract_ints(reg):
return False, i[0]
return True, ord(reg) - ord("w")
def parse_raw():
instructions = list()
for line in raw.splitlines():
opcode, *regs = line.split()
instructions.append((opcode, map(decode, regs).collect()))
return instructions
data = parse_raw()
def trunc_div(x, y):
return x // y if x >= 0 else -(-x // y)
def rem(x, y):
return x % y if x >= 0 else -(-x % y)
def get(vars, arg):
if arg[0]:
return vars[arg[1]]
return arg[1]
def optimised_monad(nums):
z = nums[0] + 3
z *= 26
z += nums[1] + 12
if True:
if nums[2] + 3 != nums[3]:
z *= 26
z += nums[3] + 12
if nums[4] - 6 != nums[5]:
z *= 26
z += nums[5] + 1
z, x = divmod(z, 26)
x -= 4
if x != nums[6]:
z *= 26
z += nums[6] + 1
z *= 26
z += nums[7] + 13
if True:
z *= 26
z += nums[8] + 1
if True:
if nums[9] - 5 != nums[10]:
z *= 26
z += nums[10] + 2
z, x = divmod(z, 26)
if x != nums[11]:
z *= 26
z += nums[11] + 11
z, x = divmod(z, 26)
x -= 8
if x != nums[12]:
z *= 26
z += nums[12] + 10
z, x = divmod(z, 26)
x -= 7
if x != nums[13]:
z *= 26
z += nums[13] + 3
return not z
def run_machine(input_queue):
vars = [0, 0, 0, 0]
for opcode, args in data:
match opcode:
case "inp":
vars[args[0][1]] = input_queue.pop(0)
case "add":
vars[args[0][1]] += get(vars, args[1])
case "mul":
vars[args[0][1]] *= get(vars, args[1])
case "div":
vars[args[0][1]] = trunc_div(get(vars, args[0]), get(vars, args[1]))
case "mod":
vars[args[0][1]] = rem(get(vars, args[0]), get(vars, args[1]))
case "eql":
vars[args[0][1]] = 1 if get(vars, args[0]) == get(vars, args[1]) else 0
return vars
def part_one():
best = None
# for digs in itertools.product(range(1, 10), repeat = 14):
# if digs[-5:] == (1,1,1,1,1):
# print(digs)
# if optimised_monad(digs):
# best = digs
nums = [9] * 14
nums[2] = 6
nums[5] = 3
nums[1] = 1
nums[10] = 4
nums[8] = 8
nums[7] = 4
nums[13] = 5
# print(optimised_monad(nums))
return int("".join(map(str, nums)))
def part_two():
nums = [1] * 14
nums[3] = 4
nums[4] = 7
nums[6] = 9
nums[9] = 6
nums[11] = 2
nums[12] = 6
nums[0] = 5
if optimised_monad(nums):
return int("".join(map(str, nums)))
# for digs in itertools.product(range(1, 10), repeat = 14):
# if optimised_monad(digs):
# return int("".join(map(str, digs)))
aoc_helper.lazy_submit(day=24, year=2021, solution=part_one)
aoc_helper.lazy_submit(day=24, year=2021, solution=part_two)