2024 - Day 3 (Mull It Over)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3

#Advent of Code
#2024 Day 3, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

import re

def solve(puzzle_input: str) -> int:
    mul_pattern: re.Pattern = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)")

    return sum(int(a) * int(b) for a, b in re.findall(mul_pattern, puzzle_input))

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_string()

    return "The sum of the results of the multiplications is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve("xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"), 161)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

#Advent of Code
#2024 Day 3, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

import re

def solve(puzzle_input: str) -> int:
    inst_pattern: re.Pattern = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)|(do\(\))|(don't\(\))")
    enabled: bool = True
    total: int = 0

    for a, b, do, dont in re.findall(inst_pattern, puzzle_input):
        if enabled and a and b:
            total += int(a) * int(b)
        elif do:
            enabled = True
        elif dont:
            enabled = False

    return total

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_string()

    return "The sum of the results of the multiplications is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"), 48)

if __name__ == "__main__":
    run(main)
Return to solution index