|
|
|
@ -1,7 +1,7 @@
@@ -1,7 +1,7 @@
|
|
|
|
|
# Example input file |
|
|
|
|
# Should return accumulated value of 3749 (part 1) |
|
|
|
|
# Should return accumulated value of 11387 (part 2) |
|
|
|
|
with open('example', 'r') as f: |
|
|
|
|
with open("example", "r") as f: |
|
|
|
|
lines = f.readlines() |
|
|
|
|
|
|
|
|
|
# # Simpler example input file |
|
|
|
@ -10,7 +10,7 @@ with open('example', 'r') as f:
@@ -10,7 +10,7 @@ with open('example', 'r') as f:
|
|
|
|
|
# lines = f.readlines() |
|
|
|
|
|
|
|
|
|
# Real input file from AoC website |
|
|
|
|
with open('input', 'r') as f: |
|
|
|
|
with open("input", "r") as f: |
|
|
|
|
lines = f.readlines() |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -30,7 +30,7 @@ add that test value to the final sum. Otherwise, don't.
@@ -30,7 +30,7 @@ add that test value to the final sum. Otherwise, don't.
|
|
|
|
|
# Convert list of strings "<test_val>: <int1> <int2>" to mapping |
|
|
|
|
test_map = {} |
|
|
|
|
for line in lines: |
|
|
|
|
if line=="": |
|
|
|
|
if line == "": |
|
|
|
|
continue |
|
|
|
|
a, b = line.strip().split(":") |
|
|
|
|
right_values = [int(j) for j in b.strip().split(" ")] |
|
|
|
@ -40,39 +40,49 @@ for line in lines:
@@ -40,39 +40,49 @@ for line in lines:
|
|
|
|
|
|
|
|
|
|
def find_valid_operators(target_value, sum_nums, operator_list, part2): |
|
|
|
|
|
|
|
|
|
if len(operator_list) == len(sum_nums)-1: |
|
|
|
|
if len(operator_list) == len(sum_nums) - 1: |
|
|
|
|
# Base case, picked all operators, now check if they yield target value |
|
|
|
|
accumulator = sum_nums[0] |
|
|
|
|
#opstr = f"{accumulator}" |
|
|
|
|
# opstr = f"{accumulator}" |
|
|
|
|
for i, op in enumerate(operator_list): |
|
|
|
|
if op=="*": |
|
|
|
|
accumulator *= sum_nums[i+1] |
|
|
|
|
elif op=="+": |
|
|
|
|
accumulator += sum_nums[i+1] |
|
|
|
|
elif op=="||" and part2 is True: |
|
|
|
|
accumulator = int(str(accumulator) + str(sum_nums[i+1])) |
|
|
|
|
if op == "*": |
|
|
|
|
accumulator *= sum_nums[i + 1] |
|
|
|
|
elif op == "+": |
|
|
|
|
accumulator += sum_nums[i + 1] |
|
|
|
|
elif op == "||" and part2 is True: |
|
|
|
|
accumulator = int(str(accumulator) + str(sum_nums[i + 1])) |
|
|
|
|
else: |
|
|
|
|
raise Exception(f"Unknown operator {op}") |
|
|
|
|
#opstr += op + str(sum_nums[i+1]) |
|
|
|
|
# opstr += op + str(sum_nums[i+1]) |
|
|
|
|
|
|
|
|
|
if accumulator == target_value: |
|
|
|
|
#print(f"Checking operator list: {target_value} = {accumulator} = {opstr} (success)") |
|
|
|
|
# print(f"Checking operator list: {target_value} = {accumulator} = {opstr} (success)") |
|
|
|
|
return True |
|
|
|
|
else: |
|
|
|
|
#print(f"Checking operator list: {target_value} != {accumulator} = {opstr} (failure)") |
|
|
|
|
# print(f"Checking operator list: {target_value} != {accumulator} = {opstr} (failure)") |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
# Recursive case |
|
|
|
|
if part2: |
|
|
|
|
return \ |
|
|
|
|
find_valid_operators(target_value, sum_nums, operator_list+["+"], part2) \ |
|
|
|
|
or find_valid_operators(target_value, sum_nums, operator_list+["*"], part2) \ |
|
|
|
|
or find_valid_operators(target_value, sum_nums, operator_list+["||"], part2) |
|
|
|
|
return ( |
|
|
|
|
find_valid_operators( |
|
|
|
|
target_value, sum_nums, operator_list + ["+"], part2 |
|
|
|
|
) |
|
|
|
|
or find_valid_operators( |
|
|
|
|
target_value, sum_nums, operator_list + ["*"], part2 |
|
|
|
|
) |
|
|
|
|
or find_valid_operators( |
|
|
|
|
target_value, sum_nums, operator_list + ["||"], part2 |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
else: |
|
|
|
|
return \ |
|
|
|
|
find_valid_operators(target_value, sum_nums, operator_list+["+"], part2) \ |
|
|
|
|
or find_valid_operators(target_value, sum_nums, operator_list+["*"], part2) |
|
|
|
|
return find_valid_operators( |
|
|
|
|
target_value, sum_nums, operator_list + ["+"], part2 |
|
|
|
|
) or find_valid_operators( |
|
|
|
|
target_value, sum_nums, operator_list + ["*"], part2 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
accumulator = 0 |
|
|
|
|
for k, right_values in test_map.items(): |
|
|
|
@ -84,7 +94,6 @@ for k, right_values in test_map.items():
@@ -84,7 +94,6 @@ for k, right_values in test_map.items():
|
|
|
|
|
print(f"Part 1: accumulated value: {accumulator}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
Part 2: |
|
|
|
|
Repeat Part 1, but include a third concatenation operator. |
|
|
|
@ -102,4 +111,3 @@ for k, right_values in test_map.items():
@@ -102,4 +111,3 @@ for k, right_values in test_map.items():
|
|
|
|
|
accumulator2 += test_value |
|
|
|
|
|
|
|
|
|
print(f"Part 2: accumulated value: {accumulator2}") |
|
|
|
|
|
|
|
|
|