1
0
Fork 0
advent-of-code/2024/day03.py

29 lines
714 B
Python
Raw Permalink Normal View History

2024-12-03 15:09:37 +01:00
#!/usr/bin/env python3
import re
with open("./2024/inputs/day03.txt") as f:
instructions = f.read()
r = re.compile("(?:mul\\(([\\d]{1,3}),([\\d]{1,3})\\))", re.MULTILINE)
matches = re.findall(r, instructions)
multiplication_total = 0
for match in matches:
n = int(match[0])
m = int(match[1])
multiplication_total += n * m
print(multiplication_total)
r2 = re.compile("(?:^|do\\(\\))+?(?:.+?)(?:don't\\(\\)|$)+?", re.DOTALL)
matches2 = re.findall(r2, instructions)
do_multiplication_total = 0
for match in matches2:
mults = re.findall(r, match)
for mult in mults:
n = int(mult[0])
m = int(mult[1])
do_multiplication_total += n * m
print(do_multiplication_total)