Advent of Code 2024 - Day 3Mull It Over
| Problem statement | Source code | Tags: String manipulation
This problem pays tribute to 2020 day 2.
Part 1
I used a regex to extract all mul(X,Y) expressions, as nature intended. The pattern is mul\((\d{1,3}),(\d{1,3})\), which captures two numbers between 1 and 999. Then I can iterate over the matches and extract the captured groups.
Part 2
Since the patterns can't nest in each other, this remains a regex matching problem, just with two additional things to match: do\(\) and don't\(\).
Each match can be one of three things: ("mul(X,Y)", Some X, Some Y), ("do()", None, None), or ("don't()", None, None). We can iterate over the matches and keep track of whether the state is enabled. Since we are folding anyway, I moved the multiplication and addition into the fold as well to avoid extra mapping and summing steps.