Advent of Code 2024 Day 3 – Mull It Over

Advent of Code 2024 Day 3 – Mull It Over

- 7 mins

Day 3: Mull It Over

Part 1

Today’s puzzle is called “Mull It Over”. The input for today’s puzzle is a file where the lines represent corrupted memory, with uncorrupted instructions and corrupted instructions. This is the example input that was given.

xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

The instructions we need to look for in the memory are mul(X,Y) where X and Y are 1-3 digit numbers. mul(X,Y) takes two arguments, X and Y, and returns the product of X and Y. In the example, there are four uncorrupted instructions: mul(2,4), mul(5,5), mul(11,8), and mul(8,5).

Our task is to find the sum of the products of the X and Y values in the uncorrupted instructions.

My Solution

For my solution, the simplest way that I could think of to extract the uncorrupted instructions is to use a regular expression to match the mul(X,Y) pattern.

import re

def get_result1(line: str) -> int:
    # Search for mul(a,b) in the line,
    # Group 1: a, Group 2: b
    pattern = r"mul\((\d{1,3}),(\d{1,3})\)"
    matches = re.finditer(pattern, line)

    return sum(int(match.group(1)) * int(match.group(2)) for match in matches)

Breaking down the code:

I’ve only included the relevant parts of the code here, but to see my full solution, you can check out my Advent of Code GitHub repository.

Part 2

For part 2, there are additional instructions in the corrupted memory that we need to consider.

There are now 2 new instructions to handle:

At the beginning of the memory, mul instructions are enabled.

Looking at a new example input:

xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

Breaking down the instructions in the example:

Our task is to find the sum of the products of the X and Y values in the uncorrupted instructions, considering the new conditional instructions.

My Solution

To solve part 2, my regex pattern needs to be updated to handle the new conditional instructions.

pattern = r"mul\((\d{1,3},\d{1,3})\)|(do\(\))|(don't\(\))"
Graphic from regex-vis.com
Regex pattern for part 2

Explanation of the updated pattern:

So this pattern will match either the mul(X,Y) instruction, do(), or don't().

Now, let’s update the get_result2 function to handle the new instructions.

def get_result2(line: str) -> int:
    pattern = r"mul\((\d{1,3},\d{1,3})\)|(do\(\))|(don't\(\))"
    matches = re.finditer(pattern, line)

    sum = 0
    enabled = True
    for match in matches:
        if match.group(1) and enabled:
            a, b = map(int, match.group(1).split(","))
            sum += a * b
        elif match.group(2):
            enabled = True
        elif match.group(3):
            enabled = False
    return sum

Breaking down the code:

This code will go through all the instructions in the memory, considering the conditional instructions to calculate the sum of the products of the X and Y values in the uncorrupted instructions.


That’s it for day 3 of Advent of Code 2024! I hope you enjoyed reading my solution and let’s see how the rest of the month goes!

Vinesh Benny

Vinesh Benny

A Software Engineer learning about different things in life and otherwise