diff --git a/py/aoc2024/day3.py b/py/aoc2024/day3.py index b1b40e89..85560f2f 100644 --- a/py/aoc2024/day3.py +++ b/py/aoc2024/day3.py @@ -11,8 +11,7 @@ xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) """ -_pattern1 = re.compile(r"mul\((\d+),(\d+)\)") -_pattern2 = re.compile(r"(?:\A|do\(\))(.*?)(?:\Z|don't\(\))", re.S) +_pattern = re.compile(r"mul\((\d+),(\d+)\)") def part1(data: str) -> int: @@ -20,7 +19,7 @@ def part1(data: str) -> int: >>> part1(SAMPLE_INPUT_1) 161 """ - return sum(int(m.group(1)) * int(m.group(2)) for m in _pattern1.finditer(data)) + return sum(int(m.group(1)) * int(m.group(2)) for m in _pattern.finditer(data)) def part2(data: str) -> int: @@ -28,7 +27,10 @@ def part2(data: str) -> int: >>> part2(SAMPLE_INPUT_2) 48 """ - return sum(part1(m.group(1)) for m in _pattern2.finditer(data)) + return sum( + part1(subdata[: subdata.index("don't()")] if "don't()" in subdata else subdata) + for subdata in data.split("do()") + ) parts = (part1, part2)