Solve 2021 day 24

This commit is contained in:
felixm 2024-11-29 16:46:01 -05:00
parent a2a6ca52d1
commit 2d3a44760e
2 changed files with 36 additions and 54 deletions

View File

@ -2,62 +2,44 @@ from lib import get_data
data = get_data(__file__)
# lines = data.splitlines()
# blocks = [lines[i:i + 18] for i in range(0, len(lines), 18)]
# blocks = zip(*blocks)
# for row in blocks:
# print("; ".join(set(row)))
def run(number=None):
inputs = []
if number is not None:
for c in str(number):
if c == "0":
return
inputs.append(int(c))
inputs = list(reversed(inputs))
else:
inputs = None
def run(number):
inputs = list(map(int, str(number)))
assert len(inputs) == 14
regs = {c: 0 for c in "wxyz"}
for i, line in enumerate(data.splitlines()):
if line.startswith("inp"):
if inputs is not None:
if inputs == []:
print(number, regs)
return
else:
op, a = line.split()
regs[a] = inputs.pop()
else:
# print(regs)
op, a = line.split()
regs[a] = int(input(f"{i // 14} > "))
continue
w, x, z = 0, 0, 0
i = 0
for i in range(len(inputs)):
w = inputs[i]
# print(f"{w=:3} {x=:3} {z=}")
op, a, b = line.split()
b = regs[b] if b in regs else int(b)
match op:
case "add":
regs[a] = regs[a] + b
case "mul":
regs[a] = regs[a] * b
case "div":
regs[a] = regs[a] // b
case "mod":
regs[a] = regs[a] % b
case "eql":
regs[a] = 1 if regs[a] == b else 0
to_add_x = [15, 15, 12, 13, -12, 10, -9, 14, 13, -14, -11, -2, -16, -14][i]
to_div_z = [1, 1, 1, 1, 26, 1, 26, 1, 1, 26, 26, 26, 26, 26][i]
to_add_z = [15, 10, 2, 16, 12, 11, 5, 16, 6, 15, 3, 12, 10, 13][i]
zz = z
x = (z % 26) + to_add_x
z = z // to_div_z
# print(f"{zz % 26=} + {to_add_x=} {x=}")
if x == w:
# print(f"added = 0")
pass
else:
z *= 26
z += w + to_add_z
# print(f"added {w=} + {to_add_z=} = {w + to_add_z}")
# print()
# print(f"\nEND:\n{w=} {x=} {z=}", "\n" * 15)
return z
print("end", inputs, number, regs)
largest_model_nr = 89959794919939
assert run(largest_model_nr) == 0
print(largest_model_nr)
run(19111111111111)
# while True:
# try:
# run()
# except KeyboardInterrupt:
# print("\n restart <")
# pass
lowest_model_nr = 17115131916112
assert run(lowest_model_nr) == 0
print(lowest_model_nr)

View File

@ -195,7 +195,7 @@ Solutions and utility script for Advent of Code challenges in Python.
- Day 21: 37:45 (Wasn't hard but I was just too slow.)
- Day 22:
- Day 23: 81:38 (Fun but obviously not competitive.)
- Day 24:
- Day 24: 232:00 (Super hard for me but I am proud of it.)
- Day 25: 15:43 (That could have been much much faster.)
## AoC 2022