Advertisement
bob_f

AOC2020D08.py

Sep 27th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1.  
  2. from collections import namedtuple
  3.  
  4. Instruction = namedtuple('Instruction', 'operation argument')
  5.  
  6. def get_puzzle_input(a_file_name: str) -> list[Instruction]:
  7.     instructions: list[Instruction] = []
  8.  
  9.     with open(a_file_name) as INFILE:
  10.         for line in INFILE:
  11.             line = line.rstrip().split()
  12.             instructions.append(Instruction(line[0], int(line[1])))
  13.    
  14.     return instructions
  15.  
  16. def solve_p1(a_instructions: list[Instruction]) -> int:
  17.     accumulator: int = 0
  18.     next_instruction = 0
  19.     instructions_executed: set[int] = set()
  20.  
  21.     while True:
  22.         if next_instruction in instructions_executed:
  23.             break
  24.  
  25.         match a_instructions[next_instruction].operation:
  26.             case 'nop':
  27.                 instructions_executed.add(next_instruction)
  28.                 next_instruction += 1
  29.             case 'acc':
  30.                 accumulator += a_instructions[next_instruction].argument
  31.                 instructions_executed.add(next_instruction)
  32.                 next_instruction += 1
  33.             case 'jmp':
  34.                 instructions_executed.add(next_instruction)
  35.                 next_instruction += a_instructions[next_instruction].argument
  36.  
  37.     return accumulator
  38.  
  39. instructions = get_puzzle_input('aoc_2020/aoc_2020_day_08.txt')
  40. # print(f"{instructions=}")
  41.  
  42. p1 = solve_p1(instructions)
  43. print(f"{p1=}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement