Advertisement
petrlos

AoC 2024/17

Dec 18th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. #Advent of Code 2024: Day 17
  2. import re
  3. from icecream import ic
  4.  
  5. def decode_combo_operand(id, regs):
  6.     combos = [0,1,2,3] + regs
  7.     return combos[id]
  8.  
  9. def parse_data(lines):
  10.     a, b, c, *program = list(map(int, (re.findall(r"\d+", lines))))
  11.     return [a, b, c], program
  12.  
  13. inst = {
  14.     0: lambda regs, combo: regs[0] // 2** combo,
  15.     1: lambda regs, combo: regs[1] ^ combo,
  16.     2: lambda combo: combo % 8,
  17.     4: lambda regs: regs[1] ^ regs[2],
  18.     5: lambda combo: combo % 8,
  19. } #6 and 7 are same like zero, only save to other register
  20.  
  21. #MAIN
  22. with open("test.txt") as file:
  23.     lines = file.read()
  24.  
  25. regs, program = parse_data(lines)
  26. output = []
  27.  
  28. print (regs, program)
  29.  
  30. pointer = 0
  31. while pointer < len(program):
  32.     operation, combo = program[pointer], program[pointer+1]
  33.     combo = decode_combo_operand(combo, regs)
  34.     if operation == 0:
  35.         regs[0] = inst[0](regs, combo)
  36.     elif operation == 1:
  37.         regs[1] = inst[1](regs, combo)
  38.     elif operation == 2:
  39.         regs[1] = inst[2](combo)
  40.     elif operation == 3:
  41.         if regs[0] != 0:
  42.             pointer = combo
  43.             continue
  44.     elif operation == 4:
  45.         regs[1] = inst[4](regs)
  46.     elif operation == 5:
  47.         output.append(str(inst[5](combo)))
  48.     elif operation == 6:
  49.         regs[1] = inst[0](regs, combo)
  50.     elif operation == 7:
  51.         regs[2] = inst[1](regs, combo)
  52.     pointer += 2
  53.  
  54. print(",".join(output))
  55. print(regs)
Tags: aoc2024/17
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement