2024 day 3.

This commit is contained in:
Mikael CAPELLE
2024-12-03 08:29:25 +01:00
parent a4ad0259a9
commit cb0145baa2
3 changed files with 36 additions and 3 deletions

View File

@@ -1,10 +1,36 @@
import re
import sys
from typing import Iterator
lines = sys.stdin.read().splitlines()
answer_1 = ...
def extract_multiply(line: str) -> Iterator[int]:
for m in re.findall(r"mul\(([0-9]{1,3}),\s*([0-9]{1,3})\)", line):
yield int(m[0]) * int(m[1])
answer_2 = ...
def remove_disabled_parts(line: str) -> str:
fixed_line, accumulate = "", True
while line:
if accumulate:
if (dont_i := line.find("don't()")) != -1:
fixed_line += line[:dont_i]
line, accumulate = line[dont_i:], False
else:
fixed_line += line
line = ""
else:
if (do_i := line.find("do()")) != -1:
line, accumulate = line[do_i:], True
else:
line = ""
return fixed_line
line = sys.stdin.read().strip()
answer_1 = sum(extract_multiply(line))
answer_2 = sum(extract_multiply(remove_disabled_parts(line)))
print(f"answer 1 is {answer_1}")
print(f"answer 2 is {answer_2}")