-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03.py
32 lines (25 loc) · 801 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
import re
from collections import deque
from common.utils import get_input
input = get_input(day=3)
def first_part():
return sum(int(x) * int(y) for x, y in re.findall("mul\((\d+),(\d+)\)", input))
def second_part():
instructions = deque(
re.findall("(?:mul\(\d+,\d+\)|do\(\)|don't\(\))", input)
)
result = 0
activated = True
while instructions:
instruction = instructions.popleft()
match instruction:
case "do()":
activated = True
case "don't()":
activated = False
case _ if activated:
x, y = re.findall("\d+", instruction)
result += int(x) * int(y)
return result
print(f"Part 1: {first_part()}")
print(f"Part 2: {second_part()}")