Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Advent of Code 2024: Day 17
- import re
- from icecream import ic
- def decode_combo_operand(id, regs):
- combos = [0,1,2,3] + regs
- return combos[id]
- def parse_data(lines):
- a, b, c, *program = list(map(int, (re.findall(r"\d+", lines))))
- return [a, b, c], program
- inst = {
- 0: lambda regs, combo: regs[0] // 2** combo,
- 1: lambda regs, combo: regs[1] ^ combo,
- 2: lambda combo: combo % 8,
- 4: lambda regs: regs[1] ^ regs[2],
- 5: lambda combo: combo % 8,
- } #6 and 7 are same like zero, only save to other register
- #MAIN
- with open("test.txt") as file:
- lines = file.read()
- regs, program = parse_data(lines)
- output = []
- print (regs, program)
- pointer = 0
- while pointer < len(program):
- operation, combo = program[pointer], program[pointer+1]
- combo = decode_combo_operand(combo, regs)
- if operation == 0:
- regs[0] = inst[0](regs, combo)
- elif operation == 1:
- regs[1] = inst[1](regs, combo)
- elif operation == 2:
- regs[1] = inst[2](combo)
- elif operation == 3:
- if regs[0] != 0:
- pointer = combo
- continue
- elif operation == 4:
- regs[1] = inst[4](regs)
- elif operation == 5:
- output.append(str(inst[5](combo)))
- elif operation == 6:
- regs[1] = inst[0](regs, combo)
- elif operation == 7:
- regs[2] = inst[1](regs, combo)
- pointer += 2
- print(",".join(output))
- print(regs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement