-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.py
More file actions
36 lines (26 loc) · 660 Bytes
/
day3.py
File metadata and controls
36 lines (26 loc) · 660 Bytes
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
import re
# part 1
# regex: mul\(\d+,\d+\)
s1 = ""
with open("d3.txt", "r") as fin:
lines = fin.readlines()
s1 = "".join(lines)
p1matches = re.findall("mul\\((\\d+),(\\d+)\\)", s1)
total = 0
for x, y in p1matches:
total += int(x) * int(y)
print(total)
# part 2
# regex: do\(\)|don't\(\)|mul\((\d+),(\d+)\)
p2matches = re.findall("do\\(\\)|don't\\(\\)|mul\\(\\d+,\\d+\)", s1)
total2 = 0
on = True
for match in p2matches:
if (match == "do()"):
on = True
elif (match == "don't()"):
on = False
elif (on):
x, y = re.findall("mul\\((\\d+),(\\d+)\\)", match)[0]
total2 += int(x) * int(y)
print(total2)