From 8277dfe0f9b098e337e8b12cdc7eae3f1c5f5bde Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 8 Dec 2020 09:43:28 +0100 Subject: [PATCH] we only need to check the instructions up to the infinite loop - everything else cannot contain the error --- day08.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/day08.py b/day08.py index eda100f..add5300 100644 --- a/day08.py +++ b/day08.py @@ -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: