Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import namedtuple
- Instruction = namedtuple('Instruction', 'operation argument')
- def get_puzzle_input(a_file_name: str) -> list[Instruction]:
- instructions: list[Instruction] = []
- with open(a_file_name) as INFILE:
- for line in INFILE:
- line = line.rstrip().split()
- instructions.append(Instruction(line[0], int(line[1])))
- return instructions
- def solve_p1(a_instructions: list[Instruction]) -> int:
- accumulator: int = 0
- next_instruction = 0
- instructions_executed: set[int] = set()
- while True:
- if next_instruction in instructions_executed:
- break
- match a_instructions[next_instruction].operation:
- case 'nop':
- instructions_executed.add(next_instruction)
- next_instruction += 1
- case 'acc':
- accumulator += a_instructions[next_instruction].argument
- instructions_executed.add(next_instruction)
- next_instruction += 1
- case 'jmp':
- instructions_executed.add(next_instruction)
- next_instruction += a_instructions[next_instruction].argument
- return accumulator
- instructions = get_puzzle_input('aoc_2020/aoc_2020_day_08.txt')
- # print(f"{instructions=}")
- p1 = solve_p1(instructions)
- print(f"{p1=}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement