-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.py
36 lines (27 loc) · 926 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
with open("input.txt", "r") as f:
inp = f.read()
def run(res, operands, part):
if res <= 0:
return False
if len(operands) == 1:
return res == operands[0]
m = False
c = False
p = False
if res % operands[-1] == 0:
m = run(res // operands[-1], operands[:-1], part)
if part == 2 and not m and str(res).endswith(str(operands[-1])) and len(str(res)) > len(str(operands[-1])):
c = run(int(str(res)[:-len(str(operands[-1]))]), operands[:-1], part)
if not m and not c:
p = run(res - operands[-1], operands[:-1], part)
return m or c or p
part1 = 0
part2 = 0
for line in inp.splitlines():
res, operands = line.split(":")
res = int(res)
operands = [int(x) for x in operands.split()]
part1 += res if run(res, operands, 1) else 0
part2 += res if run(res, operands, 2) else 0
print(part1)
print(part2)