-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
53 lines (36 loc) · 896 Bytes
/
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
44
45
46
47
48
49
50
51
52
53
import sys
import re
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
memory = ""
with open(inputfile,"r") as input:
for line in input:
memory += line.rstrip()
def run_part1():
regex = r"mul\(\d\d?\d?,\d\d?\d?\)"
ins = re.findall(regex,memory)
total = 0
for one in ins:
factors = one[4:-1].split(",")
total += int(factors[0]) * int(factors[1])
# print(memory)
print(total)
def run_part2():
regex = r"mul\(\d\d?\d?,\d\d?\d?\)|do\(\)|don't\(\)"
ins = re.findall(regex,memory)
total = 0
process = 1
for one in ins:
if one.startswith("don't"):
process = 0
elif one.startswith("do"):
process = 1
elif process:
factors = one[4:-1].split(",")
total += int(factors[0]) * int(factors[1])
print(total)
run_part1()
run_part2()