Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with open('Day8.txt', 'rt') as f:
- data = f.read().split('\n')
- instructions = {}
- for ind, instruction in enumerate(data):
- command, step = instruction.split()
- instructions[ind] = [command, int(step)]
- def procedure (instructions, key, accumulator):
- command, step = instructions.get(key)
- if command == 'nop':
- key += 1
- elif command == 'jmp':
- key += step
- else:
- key += 1
- accumulator += step
- return key, accumulator
- #Part 1
- key = 0
- accumulator = 0
- keys = list()
- # results = {}
- while True:
- key, accumulator = procedure(instructions, key, accumulator)
- if key not in keys:
- keys.append(key)
- else:
- break
- print(accumulator)
- # Part 2:
- def search_change(k, instructions):
- dict_change = {'nop': 'jmp', 'jmp': 'nop'}
- instructions_2 = {}
- key_2 = 0
- keys_2 = list()
- accu = 0
- last_key = max(instructions.keys())
- for old_value, new_value in dict_change.items():
- if instructions.get(k)[0] == old_value:
- dict_update={k:[new_value,instructions.get(k)[1]]}
- instructions_2.update(instructions)
- instructions_2.update(dict_update)
- while last_key not in keys_2:
- key_2, accu = procedure(instructions_2, key_2, accu)
- if key_2 not in keys_2:
- keys_2.append(key_2)
- else:
- break
- if keys_2[-1] == last_key:
- key_2, accu = procedure(instructions_2, key_2, accu)
- print('final_accumulator:', accu)
- break
- for k in keys:
- search_change(k, instructions)
Add Comment
Please, Sign In to add comment