-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_3.py
46 lines (37 loc) · 1.11 KB
/
day_3.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
import re
from datetime import datetime
time_start = datetime.now()
with open('day_3.input', 'r') as file:
lines = file.readlines()
total_lines = "".join(lines)
mul_pattern = r"mul\([0-9]+,[0-9]+\)"
number_pattern = r"[0-9]+"
def part1():
muls = re.findall(mul_pattern, total_lines).__str__()
numbers = re.findall(number_pattern, muls)
total = 0
for i in range(0, len(numbers), 2):
total += int(numbers[i]) * int(numbers[i+1])
print(total)
def remove_parts(line):
start = line.find("don't()")
while start != -1:
rest = line[start:]
end = rest.find("do()")
if end == -1:
return line[:start]
line = line[:start] + rest[end+4:]
start = line.find("don't()")
return line
def part2():
fixed = remove_parts(total_lines)
muls = re.findall(mul_pattern, fixed).__str__()
numbers = re.findall(number_pattern, muls)
total = 0
for i in range(0, len(numbers), 2):
total += int(numbers[i]) * int(numbers[i+1])
print(total)
part1()
part2()
time_end = datetime.now()
print("Total time: ", time_end - time_start)