-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.py
38 lines (29 loc) · 986 Bytes
/
day07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from operator import add, mul
from aoc import get_input
puzzle = get_input(day=7)
def concat(a, b):
return int(str(a)+str(b))
def dfs(numbers, operators, intermediate, ans):
if not numbers: # reached the bottom of the DFS
return intermediate
if intermediate > ans: # all operators make things larger, so no use in continuing to search
return float('inf')
for op in operators:
result = dfs(numbers[1:], operators, op(intermediate, numbers[0]), ans)
if result == ans:
return result
return float('inf')
total1 = 0
total2 = 0
for line in puzzle:
ans, operands = line.split(':')
ans = int(ans)
operands = [int(p) for p in operands.strip().split()]
result = dfs(operands[1:], (mul, add), operands[0], ans)
if result == ans:
total1 += result
result = dfs(operands[1:], (mul, add, concat), operands[0], ans)
if result == ans:
total2 += result
print(total1)
print(total2)