Created
December 8, 2020 16:57
-
-
Save pjhoberman/3ef18ba1567242525024ec590db38136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('input.txt') as file: | |
instructions = file.read().splitlines() | |
used = [] | |
idx = 0 | |
acc = 0 | |
while idx not in used: | |
used.append(idx) | |
op, arg = instructions[idx].split() | |
if op == "nop": | |
idx += 1 | |
continue | |
if op == "acc": | |
acc += int(arg) | |
idx += 1 | |
continue | |
if op == "jmp": | |
idx += int(arg) | |
assert acc == 5 | |
## step 2 | |
with open('input.txt') as file: | |
instructions = file.read().splitlines() | |
for i, instruction in enumerate(instructions): | |
instructions_copy = instructions.copy() | |
if instruction.split()[0] == "nop": | |
instructions_copy[i] = instructions_copy[i].replace('nop', 'jmp') | |
elif instruction.split()[0] == "jmp": | |
instructions_copy[i] = instructions_copy[i].replace('jmp', 'nop') | |
try: | |
used = [] | |
idx = 0 | |
acc = 0 | |
while idx not in used: | |
used.append(idx) | |
op, arg = instructions_copy[idx].split() | |
if op == "nop": | |
idx += 1 | |
continue | |
if op == "acc": | |
acc += int(arg) | |
idx += 1 | |
continue | |
if op == "jmp": | |
idx += int(arg) | |
except IndexError: | |
print("made it") | |
print(acc) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment