Solve 2021 day 24

This commit is contained in:
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__) 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): def run(number):
inputs = [] inputs = list(map(int, str(number)))
if number is not None: assert len(inputs) == 14
for c in str(number):
if c == "0":
return
inputs.append(int(c))
inputs = list(reversed(inputs))
else:
inputs = None
regs = {c: 0 for c in "wxyz"} w, x, z = 0, 0, 0
for i, line in enumerate(data.splitlines()): i = 0
if line.startswith("inp"): for i in range(len(inputs)):
if inputs is not None: w = inputs[i]
if inputs == []: # print(f"{w=:3} {x=:3} {z=}")
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
op, a, b = line.split() to_add_x = [15, 15, 12, 13, -12, 10, -9, 14, 13, -14, -11, -2, -16, -14][i]
b = regs[b] if b in regs else int(b) to_div_z = [1, 1, 1, 1, 26, 1, 26, 1, 1, 26, 26, 26, 26, 26][i]
match op: to_add_z = [15, 10, 2, 16, 12, 11, 5, 16, 6, 15, 3, 12, 10, 13][i]
case "add":
regs[a] = regs[a] + b zz = z
case "mul": x = (z % 26) + to_add_x
regs[a] = regs[a] * b z = z // to_div_z
case "div":
regs[a] = regs[a] // b # print(f"{zz % 26=} + {to_add_x=} {x=}")
case "mod":
regs[a] = regs[a] % b if x == w:
case "eql": # print(f"added = 0")
regs[a] = 1 if regs[a] == b else 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)
lowest_model_nr = 17115131916112
run(19111111111111) assert run(lowest_model_nr) == 0
print(lowest_model_nr)
# while True:
# try:
# run()
# except KeyboardInterrupt:
# print("\n restart <")
# pass

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 21: 37:45 (Wasn't hard but I was just too slow.)
- Day 22: - Day 22:
- Day 23: 81:38 (Fun but obviously not competitive.) - 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.) - Day 25: 15:43 (That could have been much much faster.)
## AoC 2022 ## AoC 2022