Skip to content

Commit

Permalink
Reuse part 1 in part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 3, 2024
1 parent 6ae1dfb commit 0f3e7f2
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions py/aoc2024/day3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

_pattern1 = re.compile(r"mul\((\d+),(\d+)\)")
_pattern2 = re.compile(r"(do\(\))|(don't\(\))|mul\((\d+),(\d+)\)")
_pattern2 = re.compile(r"(?:\A|do\(\))(.*?)(?:\Z|don't\(\))", re.S)


def part1(data: str) -> int:
Expand All @@ -28,15 +28,7 @@ def part2(data: str) -> int:
>>> part2(SAMPLE_INPUT_2)
48
"""
enabled, total = True, 0
for m in _pattern2.finditer(data):
if m.group(1):
enabled = True
elif m.group(2):
enabled = False
elif enabled:
total += int(m.group(3)) * int(m.group(4))
return total
return sum(part1(m.group(1)) for m in _pattern2.finditer(data))


parts = (part1, part2)

0 comments on commit 0f3e7f2

Please sign in to comment.