pre-optimizing the opcode makes p1 take twice the time, but p2 almost twice as fast
This commit is contained in:
parent
8277dfe0f9
commit
a8a0873eec
33
day08.py
33
day08.py
@ -5,6 +5,16 @@ TEST_SOLUTION_PART1 = 5
|
|||||||
TEST_SOLUTION_PART2 = 8
|
TEST_SOLUTION_PART2 = 8
|
||||||
|
|
||||||
|
|
||||||
|
def optimizeInput(input_array):
|
||||||
|
return_array = []
|
||||||
|
for line in input_array:
|
||||||
|
command, param = line.split()
|
||||||
|
param = int(param)
|
||||||
|
return_array.append((command, param))
|
||||||
|
|
||||||
|
return return_array
|
||||||
|
|
||||||
|
|
||||||
def runCode(lines, infinite_returns_value=False, infinite_returns_set=False):
|
def runCode(lines, infinite_returns_value=False, infinite_returns_set=False):
|
||||||
accumulator = 0
|
accumulator = 0
|
||||||
line_pointer = 0
|
line_pointer = 0
|
||||||
@ -20,34 +30,35 @@ def runCode(lines, infinite_returns_value=False, infinite_returns_set=False):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
lines_executed.add(line_pointer)
|
lines_executed.add(line_pointer)
|
||||||
command, param = lines[line_pointer].split()
|
command, param = lines[line_pointer]
|
||||||
if command == 'nop':
|
if command == 'nop':
|
||||||
line_pointer += 1
|
line_pointer += 1
|
||||||
elif command == 'acc':
|
elif command == 'acc':
|
||||||
accumulator = accumulator + int(param)
|
accumulator = accumulator + param
|
||||||
line_pointer += 1
|
line_pointer += 1
|
||||||
elif command == 'jmp':
|
elif command == 'jmp':
|
||||||
line_pointer = line_pointer + int(param)
|
line_pointer = line_pointer + param
|
||||||
|
|
||||||
return accumulator
|
return accumulator
|
||||||
|
|
||||||
|
|
||||||
def part1(test_mode=False):
|
def part1(test_mode=False):
|
||||||
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
my_input = aoclib.getInputAsArray(day=DAY, test=test_mode)
|
||||||
return runCode(my_input, infinite_returns_value=True)
|
return runCode(optimizeInput(my_input), infinite_returns_value=True)
|
||||||
|
|
||||||
|
|
||||||
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 exec_line in runCode(my_input, infinite_returns_set=True):
|
opt_input = optimizeInput(my_input)
|
||||||
if not my_input[exec_line].startswith('nop') and not my_input[exec_line].startswith('jmp'):
|
for exec_line in runCode(opt_input, infinite_returns_set=True):
|
||||||
|
if opt_input[exec_line][0] not in ['nop', 'jmp']:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
check_code = my_input[:]
|
check_code = opt_input[:]
|
||||||
if my_input[exec_line].startswith('jmp'):
|
if opt_input[exec_line][0] == 'jmp':
|
||||||
check_code[exec_line] = check_code[exec_line].replace('jmp', 'nop')
|
check_code[exec_line] = ('nop', check_code[exec_line][1])
|
||||||
elif my_input[exec_line].startswith('nop'):
|
elif opt_input[exec_line][0] == 'nop':
|
||||||
check_code[exec_line] = check_code[exec_line].replace('nop', 'jmp')
|
check_code[exec_line] = ('jmp', check_code[exec_line][1])
|
||||||
|
|
||||||
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