Merge remote-tracking branch 'origin/master'

This commit is contained in:
Stefan Harmuth 2020-12-09 06:21:55 +01:00
commit 9391bc2b12
2 changed files with 25 additions and 20 deletions

View File

@ -21,11 +21,8 @@ def getBagContent(bag_line):
return return_dict
for content in contents.split(", "):
match_groups = re.match(content_re, content)
if not match_groups:
print("ERROR: content '%s' cannot be matched" % content)
return_dict[color][match_groups.group(2)] = int(match_groups.group(1))
content_count, content_color = re.match(content_re, content).groups()
return_dict[color][content_color] = int(content_count)
return return_dict

View File

@ -5,7 +5,17 @@ TEST_SOLUTION_PART1 = 5
TEST_SOLUTION_PART2 = 8
def runCode(lines, infinite_returns_value=False):
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):
accumulator = 0
line_pointer = 0
lines_executed = set()
@ -14,14 +24,14 @@ 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
lines_executed.add(line_pointer)
command, param = lines[line_pointer].split()
param = int(param)
command, param = lines[line_pointer]
if command == 'nop':
pass
line_pointer += 1
elif command == 'acc':
accumulator = accumulator + param
@ -34,23 +44,21 @@ def runCode(lines, infinite_returns_value=False):
def part1(test_mode=False):
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):
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'):
opt_input = optimizeInput(my_input)
for exec_line in runCode(opt_input, infinite_returns_set=True):
if opt_input[exec_line][0] not in ['nop', '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_code = opt_input[:]
if opt_input[exec_line][0] == 'jmp':
check_code[exec_line] = ('nop', check_code[exec_line][1])
elif opt_input[exec_line][0] == 'nop':
check_code[exec_line] = ('jmp', check_code[exec_line][1])
check_answer = runCode(check_code)
if check_answer is not None: