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() if command == 'nop': line_pointer += 1 elif command == 'acc': accumulator = accumulator + int(param) line_pointer += 1 elif command == 'jmp': line_pointer = line_pointer + int(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) for counter, check_command in enumerate(my_input): if not check_command.startswith('nop') and not check_command.startswith('jmp'): continue check_code = my_input[:] 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