58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import aoclib
|
|
|
|
DAY = 8
|
|
TEST_SOLUTION_PART1 = 5
|
|
TEST_SOLUTION_PART2 = 8
|
|
|
|
|
|
def runCode(lines, infinite_returns_value=False):
|
|
accumulator = 0
|
|
line_pointer = 0
|
|
lines_executed = set()
|
|
last_line = len(lines)
|
|
while line_pointer < last_line:
|
|
if line_pointer in lines_executed:
|
|
if infinite_returns_value:
|
|
return accumulator
|
|
else:
|
|
return None
|
|
|
|
lines_executed.add(line_pointer)
|
|
command, param = lines[line_pointer].split()
|
|
param = int(param)
|
|
if command == 'nop':
|
|
pass
|
|
line_pointer += 1
|
|
elif command == 'acc':
|
|
accumulator = accumulator + param
|
|
line_pointer += 1
|
|
elif command == 'jmp':
|
|
line_pointer = line_pointer + param
|
|
|
|
return accumulator
|
|
|
|
|
|
def part1(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
|
return runCode(my_input, infinite_returns_value=True)
|
|
|
|
|
|
def part2(test_mode=False):
|
|
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
|
counter = -1
|
|
for check_command in my_input:
|
|
counter += 1
|
|
if not check_command.startswith('nop') and not check_command.startswith('jmp'):
|
|
continue
|
|
|
|
check_code = my_input.copy()
|
|
if check_command.startswith('nop'):
|
|
check_code[counter] = check_code[counter].replace('nop', 'jmp')
|
|
|
|
if check_command.startswith('jmp'):
|
|
check_code[counter] = check_code[counter].replace('jmp', 'nop')
|
|
|
|
check_answer = runCode(check_code)
|
|
if check_answer is not None:
|
|
return check_answer
|