Solve 2024 till day 18
This commit is contained in:
61
2024/d17.py
Normal file
61
2024/d17.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from lib import get_data
|
||||
from lib import ints
|
||||
|
||||
|
||||
data = get_data(__file__)
|
||||
nums, prog = data.split("\n\n")
|
||||
regs = ints(nums)
|
||||
prog = ints(prog)
|
||||
|
||||
i = 0
|
||||
out = []
|
||||
while i < len(prog) - 1:
|
||||
inst, op = prog[i], prog[i + 1]
|
||||
assert op != 7
|
||||
combo = op if op not in [4, 5, 6] else regs[op - 4]
|
||||
literal = op
|
||||
match inst:
|
||||
case 0:
|
||||
regs[0] = regs[0] >> combo
|
||||
case 1:
|
||||
regs[1] = regs[1] ^ literal
|
||||
case 2:
|
||||
regs[1] = combo % 8
|
||||
case 3:
|
||||
if regs[0] != 0:
|
||||
i = literal - 2
|
||||
case 4:
|
||||
regs[1] = regs[1] ^ regs[2]
|
||||
case 5:
|
||||
out.append(combo % 8)
|
||||
case 6:
|
||||
regs[1] = regs[0] // (2**combo)
|
||||
case 7:
|
||||
regs[2] = regs[0] // (2**combo)
|
||||
case _:
|
||||
assert False
|
||||
i += 2
|
||||
|
||||
print(",".join(map(str, out)))
|
||||
|
||||
|
||||
def get_a(a_current, xs):
|
||||
if len(xs) == 0:
|
||||
return [a_current // 8]
|
||||
x = xs[0]
|
||||
aan = []
|
||||
for a_new in range(8):
|
||||
a = a_current + a_new
|
||||
b = a % 8
|
||||
assert b == a_new
|
||||
b = b ^ 1
|
||||
c = a >> b
|
||||
b = b ^ c
|
||||
b = b ^ 5
|
||||
if (b % 8) == x:
|
||||
aan += get_a(a << 3, xs[1:])
|
||||
return aan
|
||||
|
||||
|
||||
aa = get_a(0, list(reversed(prog)))
|
||||
print(min(aa))
|
||||
Reference in New Issue
Block a user