Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
9391bc2b12
7
day07.py
7
day07.py
@ -21,11 +21,8 @@ def getBagContent(bag_line):
|
|||||||
return return_dict
|
return return_dict
|
||||||
|
|
||||||
for content in contents.split(", "):
|
for content in contents.split(", "):
|
||||||
match_groups = re.match(content_re, content)
|
content_count, content_color = re.match(content_re, content).groups()
|
||||||
if not match_groups:
|
return_dict[color][content_color] = int(content_count)
|
||||||
print("ERROR: content '%s' cannot be matched" % content)
|
|
||||||
|
|
||||||
return_dict[color][match_groups.group(2)] = int(match_groups.group(1))
|
|
||||||
|
|
||||||
return return_dict
|
return return_dict
|
||||||
|
|
||||||
|
|||||||
38
day08.py
38
day08.py
@ -5,7 +5,17 @@ TEST_SOLUTION_PART1 = 5
|
|||||||
TEST_SOLUTION_PART2 = 8
|
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
|
accumulator = 0
|
||||||
line_pointer = 0
|
line_pointer = 0
|
||||||
lines_executed = set()
|
lines_executed = set()
|
||||||
@ -14,14 +24,14 @@ 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
|
||||||
|
|
||||||
lines_executed.add(line_pointer)
|
lines_executed.add(line_pointer)
|
||||||
command, param = lines[line_pointer].split()
|
command, param = lines[line_pointer]
|
||||||
param = int(param)
|
|
||||||
if command == 'nop':
|
if command == 'nop':
|
||||||
pass
|
|
||||||
line_pointer += 1
|
line_pointer += 1
|
||||||
elif command == 'acc':
|
elif command == 'acc':
|
||||||
accumulator = accumulator + param
|
accumulator = accumulator + param
|
||||||
@ -34,23 +44,21 @@ def runCode(lines, infinite_returns_value=False):
|
|||||||
|
|
||||||
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)
|
||||||
counter = -1
|
opt_input = optimizeInput(my_input)
|
||||||
for check_command in my_input:
|
for exec_line in runCode(opt_input, infinite_returns_set=True):
|
||||||
counter += 1
|
if opt_input[exec_line][0] not in ['nop', 'jmp']:
|
||||||
if not check_command.startswith('nop') and not check_command.startswith('jmp'):
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
check_code = my_input[:]
|
check_code = opt_input[:]
|
||||||
if check_command.startswith('nop'):
|
if opt_input[exec_line][0] == 'jmp':
|
||||||
check_code[counter] = check_code[counter].replace('nop', 'jmp')
|
check_code[exec_line] = ('nop', check_code[exec_line][1])
|
||||||
|
elif opt_input[exec_line][0] == 'nop':
|
||||||
if check_command.startswith('jmp'):
|
check_code[exec_line] = ('jmp', check_code[exec_line][1])
|
||||||
check_code[counter] = check_code[counter].replace('jmp', 'nop')
|
|
||||||
|
|
||||||
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