diff --git a/2024/src/aoc/days/day3.py b/2024/src/aoc/days/day3.py new file mode 100644 index 0000000..16c835d --- /dev/null +++ b/2024/src/aoc/days/day3.py @@ -0,0 +1,25 @@ +import re + +from . import SeparateRunner + + +class DayRunner(SeparateRunner): + @classmethod + def part1(cls, data: str) -> int: + return sum(int(a) * int(b) for a, b in re.findall(r"mul\((\d+),(\d+)\)", data)) + + @classmethod + def part2(cls, data: str) -> int: + do = True + total = 0 + for op, a, b in re.findall(r"(don't\(\)|do\(\)|mul\((\d+),(\d+)\))", data): + match op: + case "do()": + do = True + case "don't()": + do = False + case _: + if do: + total += int(a) * int(b) + + return total diff --git a/2024/tests/test_day3.py b/2024/tests/test_day3.py new file mode 100644 index 0000000..427093c --- /dev/null +++ b/2024/tests/test_day3.py @@ -0,0 +1,12 @@ +from aoc.days.day3 import DayRunner + +SAMPLE_1 = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))" +SAMPLE_2 = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" + + +def test_sample_part1() -> None: + assert DayRunner.part1(SAMPLE_1) == 161 + + +def test_sample_part2() -> None: + assert DayRunner.part2(SAMPLE_2) == 48