-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03.py
34 lines (27 loc) · 952 Bytes
/
day_03.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
import re
from models.aoc_solution import AOCSolution
class Day03(AOCSolution):
EXPECTED = {
"part_one": {"sample": 161, "data": 181345830},
"part_two": {"sample": 48, "data": 98729041},
}
def part_one(self) -> int:
multiplications = re.findall(r"mul\((\d+),(\d+)\)", self.data, re.IGNORECASE)
return sum(int(a) * int(b) for a, b in multiplications)
def part_two(self) -> int:
instructions = re.findall(
r"mul\((\d+),(\d+)\)|(do)\(\)|(don't)\(\)", self.data, re.IGNORECASE
)
total = 0
enabled = True
for instruction in instructions:
if "do" in instruction:
enabled = True
elif "don't" in instruction:
enabled = False
elif enabled:
a, b, *_ = instruction
total += int(a) * int(b)
return total
if __name__ == "__main__":
Day03().run()