we only need to check the instructions up to the infinite loop - everything else cannot contain the error

This commit is contained in:
Stefan Harmuth 2020-12-08 09:43:28 +01:00
parent 761e3bf975
commit 8277dfe0f9

View File

@ -5,7 +5,7 @@ TEST_SOLUTION_PART1 = 5
TEST_SOLUTION_PART2 = 8
def runCode(lines, infinite_returns_value=False):
def runCode(lines, infinite_returns_value=False, infinite_returns_set=False):
accumulator = 0
line_pointer = 0
lines_executed = set()
@ -14,6 +14,8 @@ def runCode(lines, infinite_returns_value=False):
if line_pointer in lines_executed:
if infinite_returns_value:
return accumulator
elif infinite_returns_set:
return lines_executed
else:
return None
@ -37,15 +39,15 @@ def part1(test_mode=False):
def part2(test_mode=False):
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
for counter, check_command in enumerate(my_input):
if not check_command.startswith('nop') and not check_command.startswith('jmp'):
for exec_line in runCode(my_input, infinite_returns_set=True):
if not my_input[exec_line].startswith('nop') and not my_input[exec_line].startswith('jmp'):
continue
check_code = my_input[:]
if check_command.startswith('jmp'):
check_code[counter] = check_code[counter].replace('jmp', 'nop')
elif check_command.startswith('nop'):
check_code[counter] = check_code[counter].replace('nop', 'jmp')
if my_input[exec_line].startswith('jmp'):
check_code[exec_line] = check_code[exec_line].replace('jmp', 'nop')
elif my_input[exec_line].startswith('nop'):
check_code[exec_line] = check_code[exec_line].replace('nop', 'jmp')
check_answer = runCode(check_code)
if check_answer is not None: