-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
43 lines (30 loc) · 1.05 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
from icecream import ic
import re
def read_data(filename: str) -> str:
with open(filename, "r") as data_file:
return data_file.read().strip()
def part1(filename: str) -> int:
values = read_data(filename)
p = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)")
return sum(int(a)*int(b) for a, b in p.findall(values))
# return sum(int(m.group(1))*int(m.group(2)) for m in p.finditer(values))
def part2(filename: str) -> int:
values = read_data(filename)
p = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don't\(\)")
s = 0
enable = True
for m in p.finditer(values):
match m.group():
case 'do()':
enable = True
case "don't()":
enable = False
case _:
if enable:
a, b = int(m.group(1)), int(m.group(2))
s += a*b
return s
assert ic(part1('./sample.txt')) == 161
assert ic(part1('./input.txt')) == 189600467
assert ic(part2('./sample2.txt')) == 48
assert ic(part2('./input.txt')) == 107069718