-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03.py
More file actions
37 lines (25 loc) · 1.2 KB
/
Copy pathDay03.py
File metadata and controls
37 lines (25 loc) · 1.2 KB
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
import re
mul_regex = re.compile(r'(mul\([0-9]+,[0-9]+\))')
def readin_data(path : str, with_instructions : bool = False) -> list[(str)]:
with open(path, 'r') as f:
content = f.read()
if with_instructions:
# clear special characters
content = re.sub(r"([^0-9a-zA-Z,()']+)", '', content)
# clear everything between "don't()" and do()
content = re.sub(r"(?<=don't\(\))(.*?)((?=do\(\))|(?=don't)|$)", '', content)
return mul_regex.findall(content)
def mulitpy(formula : str) -> int:
regex = re.compile(r'([0-9]+)')
factors = regex.findall(formula)
if len(factors) != 2:
raise Exception(f'Invalid formula "{formula}"')
else:
return int(factors[0]) * int(factors[1])
def sum_of_multiplications(multiplications : list[(str)]) -> int:
return sum([mulitpy(mul) for mul in multiplications])
if __name__ == '__main__':
multiplications = readin_data('Input/Day03.txt')
print(f'Sum of multiplications {sum_of_multiplications(multiplications)}')
multiplications = readin_data('Input/Day03.txt', with_instructions=True)
print(f'Sum of multiplications with instructions {sum_of_multiplications(multiplications)}')