Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- def get_puzzle_input(a_file_name: str) -> list[list[str]]:
- with open(a_file_name) as INFILE:
- return [line.split() for line in INFILE if not line.startswith('#')]
- def solve(a_instructions: list, a_data: defaultdict, a_part=1) -> int:
- next_instruction_index = 0
- if a_part == 2:
- a_data['c'] = 1
- while next_instruction_index < len(a_instructions):
- next_instruction = a_instructions[next_instruction_index][0]
- if next_instruction == 'cpy':
- arg1 = a_instructions[next_instruction_index][1]
- arg2 = a_instructions[next_instruction_index][2]
- a_data[arg2] = int(arg1) if arg1.isdigit() else a_data[arg1]
- next_instruction_index +=1
- elif next_instruction == 'inc':
- arg1 = a_instructions[next_instruction_index][1]
- a_data[arg1] += 1
- next_instruction_index +=1
- elif next_instruction == 'dec':
- arg1 = a_instructions[next_instruction_index][1]
- a_data[arg1] -= 1
- next_instruction_index +=1
- elif next_instruction == 'jnz':
- arg1 = a_instructions[next_instruction_index][1]
- arg2 = a_instructions[next_instruction_index][2]
- arg1_value = int(arg1) if arg1.isdigit() else a_data[arg1]
- if arg1_value == 0:
- next_instruction_index += 1
- else:
- next_instruction_index+= int(arg2)
- else:
- assert 1 == 0, 'Nah'
- return a_data['a']
- data = defaultdict(int)
- instructions = get_puzzle_input('AOC2016\AOC2016D12.txt')
- print(f'p1: {solve(instructions, data)}')
- print(f'p2: {solve(instructions, data, a_part=2)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement