-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
32 lines (26 loc) · 850 Bytes
/
day03.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
from math import prod
import re
def day03(inp):
data = ' '.join(inp.strip().splitlines())
part1 = part2 = 0
all_matcher = re.compile(r"do\(\)|don't\(\)|mul\((\d{1,3}),(\d{1,3})\)")
enabled = True
for match in all_matcher.finditer(data):
if match.group() == 'do()':
# part 2
enabled = True
elif match.group() == "don't()":
# part 2
enabled = False
else:
contribution = prod(map(int, match.groups()))
part1 += contribution
if enabled:
part2 += contribution
return part1, part2
if __name__ == "__main__":
testinp = open('day03.testinp').read()
testinp2 = open('day03.testinp2').read()
print(day03(testinp)[0], day03(testinp2)[1])
inp = open('day03.inp').read()
print(day03(inp))