27 lines
537 B
Python
27 lines
537 B
Python
|
|
import re
|
||
|
|
from math import prod
|
||
|
|
|
||
|
|
|
||
|
|
def solve(data):
|
||
|
|
needle = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)")
|
||
|
|
|
||
|
|
p1 = sum(prod(map(int, factors)) for factors in re.findall(needle, data))
|
||
|
|
|
||
|
|
p2 = sum(
|
||
|
|
sum(prod(map(int, factors)) for factors in re.findall(needle, chunk))
|
||
|
|
for chunk in data.split("do")
|
||
|
|
if not chunk.startswith("n't")
|
||
|
|
)
|
||
|
|
|
||
|
|
return p1, p2
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
with open("./input/03.txt", "r") as f:
|
||
|
|
inp = f.read().strip()
|
||
|
|
|
||
|
|
p1, p2 = solve(inp)
|
||
|
|
|
||
|
|
print(p1)
|
||
|
|
print(p2)
|