diff --git a/aoc_cas/aoc2024/day3.py b/aoc_cas/aoc2024/day3.py new file mode 100644 index 0000000..5a9b07e --- /dev/null +++ b/aoc_cas/aoc2024/day3.py @@ -0,0 +1,29 @@ +import re + +from aoc_cas.cli import solve + +MUL_REGEX = re.compile(r"mul\((\d+,\d+)\)") +DO_DONT_REGEX = re.compile(r"((don't\(\)).+?(do\(\)))|don't\(\).+?$") + + +def _sum_mults(data: str) -> int: + result = 0 + for match in MUL_REGEX.findall(data): + n1, n2 = map(int, match.split(",")) + result += n1 * n2 + return result + + +def part_a(data: str) -> int: + return _sum_mults(data) + + +def part_b(data: str) -> int: + donts_removed = DO_DONT_REGEX.sub("", data.replace("\n", "")) + return _sum_mults(donts_removed) + + +if __name__ == "__main__": + from aoc_cas.util import solve_with_examples + + solve_with_examples(year=2024, day=3) diff --git a/tests/fixtures/2024/3.txt b/tests/fixtures/2024/3.txt new file mode 100644 index 0000000..a267c8b --- /dev/null +++ b/tests/fixtures/2024/3.txt @@ -0,0 +1,8 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) +mul +- +161 +=== +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) +- +48 \ No newline at end of file