-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
46 lines (32 loc) · 1.04 KB
/
day3.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
44
45
# --- Day 3: Mull It Over ---
# https://adventofcode.com/2024/day/3
import re
def read(filename):
with open(filename, 'r') as file:
return file.read()
def day_3(filename):
pattern = r"mul\((\d{1,3}),(\d{1,3})\)"
content = read(filename)
product_sum = 0
matches = re.findall(pattern, content)
for match in matches:
product_sum += int(match[0]) * int(match[1])
return product_sum
def day_3_part_2(filename):
pattern = r"mul\(\d{1,3},\d{1,3}\)|(?:don't\(\))|(?:do\(\))"
content = read(filename)
product_sum = 0
matches = re.findall(pattern, content)
do = True
for match in matches:
print(match)
if do and match.startswith("mul"):
start = match.find("(") + 1
end = match.find(")")
innards = match[start:end]
product_sum += int(innards.split(",")[0]) * int(innards.split(",")[1])
elif match.startswith("do("):
do = True
elif match.startswith("don't"):
do = False
return product_sum