we only need to check the instructions up to the infinite loop - everything else cannot contain the error
This commit is contained in:
parent
761e3bf975
commit
8277dfe0f9
16
day08.py
16
day08.py
@ -5,7 +5,7 @@ TEST_SOLUTION_PART1 = 5
|
|||||||
TEST_SOLUTION_PART2 = 8
|
TEST_SOLUTION_PART2 = 8
|
||||||
|
|
||||||
|
|
||||||
def runCode(lines, infinite_returns_value=False):
|
def runCode(lines, infinite_returns_value=False, infinite_returns_set=False):
|
||||||
accumulator = 0
|
accumulator = 0
|
||||||
line_pointer = 0
|
line_pointer = 0
|
||||||
lines_executed = set()
|
lines_executed = set()
|
||||||
@ -14,6 +14,8 @@ def runCode(lines, infinite_returns_value=False):
|
|||||||
if line_pointer in lines_executed:
|
if line_pointer in lines_executed:
|
||||||
if infinite_returns_value:
|
if infinite_returns_value:
|
||||||
return accumulator
|
return accumulator
|
||||||
|
elif infinite_returns_set:
|
||||||
|
return lines_executed
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -37,15 +39,15 @@ def part1(test_mode=False):
|
|||||||
|
|
||||||
def part2(test_mode=False):
|
def part2(test_mode=False):
|
||||||
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
||||||
for counter, check_command in enumerate(my_input):
|
for exec_line in runCode(my_input, infinite_returns_set=True):
|
||||||
if not check_command.startswith('nop') and not check_command.startswith('jmp'):
|
if not my_input[exec_line].startswith('nop') and not my_input[exec_line].startswith('jmp'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
check_code = my_input[:]
|
check_code = my_input[:]
|
||||||
if check_command.startswith('jmp'):
|
if my_input[exec_line].startswith('jmp'):
|
||||||
check_code[counter] = check_code[counter].replace('jmp', 'nop')
|
check_code[exec_line] = check_code[exec_line].replace('jmp', 'nop')
|
||||||
elif check_command.startswith('nop'):
|
elif my_input[exec_line].startswith('nop'):
|
||||||
check_code[counter] = check_code[counter].replace('nop', 'jmp')
|
check_code[exec_line] = check_code[exec_line].replace('nop', 'jmp')
|
||||||
|
|
||||||
check_answer = runCode(check_code)
|
check_answer = runCode(check_code)
|
||||||
if check_answer is not None:
|
if check_answer is not None:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user