Solve 2021 day 20, 21, 23, 25

This commit is contained in:
2024-11-27 17:31:52 -05:00
parent 4acc229c5a
commit a2a6ca52d1
8 changed files with 388 additions and 1 deletions

63
2021/d24.py Normal file
View File

@@ -0,0 +1,63 @@
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
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
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
print("end", inputs, number, regs)
run(19111111111111)
# while True:
# try:
# run()
# except KeyboardInterrupt:
# print("\n restart <")
# pass