AoC 2024 Day 03: Mull It Over

Dated Jul 2, 2025; last modified on Wed, 02 Jul 2025

Data

The computer appears to be trying to run a program, but its memory is corrupted. All of the instructions have been jumbled up!

It seems like the goal of the program is just to multiply some numbers. It does that with instructions like mul(X,Y) where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 and 46 to get a result of 2024.

However, because the program’s memory has been corrupted, there are also invalid characters that should be ignored, even if they look like a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), or mul ( 2 , 4 ) do nothing.

For example, consider the following section of corrupted memory:

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

Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces \(2 \times 4 + 5 \times 5 + 11 \times 8 + 8 \times 5 = 161\).

To parse:

using System.Text.RegularExpressions;

namespace AoC2024;

public static partial class MullItOver
{
    private static IEnumerable<ICommand> ParseCommands(string filePath)
    {
        using StreamReader inputReader = new(filePath);
        string? line;
        while((line = inputReader.ReadLine()) != null)
        {
            var commands = InputLineRegex()
                .Matches(line)
                .Select(ParseCommand);
            
            foreach (var command in commands)
                yield return command;
        }
    }

    private static ICommand ParseCommand(Match m)
    {
        var groups = m.Groups;

        if (groups["do"].Success)
        {
            return new DoCommand();
        }
        else if (groups["dont"].Success)
        {
            return new DontCommand();
        }
        else if (groups["mul"].Success)
        {
            return new MultiplyCommand(
                int.Parse(groups["num1"].Value),
                int.Parse(groups["num2"].Value));
        }

        throw new ArgumentException($"Unrecognized match found {m}");
    }

    private interface ICommand;

    private record MultiplyCommand(int Num1, int Num2) : ICommand;
    private record DoCommand : ICommand;
    private record DontCommand : ICommand;

    [GeneratedRegex(@"(?<dont>don't\(\))|(?<do>do\(\))|(?<mul>mul\((?<num1>\d{1,3}),(?<num2>\d{1,3})\))")]
    private static partial Regex InputLineRegex();
}

Part One

Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?

namespace AoC2024;

public static partial class MullItOver
{
    public static int PartOne(string filePath)
    {
        return ParseCommands(filePath)
            .OfType<MultiplyCommand>()
            .Select(cmd => cmd.Num1 * cmd.Num2)
            .Sum();
    }
}

Part Two

There are two new instructions you’ll need to handle:

  • The do() instruction enables future mul instructions.
  • The don't() instruction disables future mul instructions.

Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.

For example:

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

The mul(5,5) and mull(11,8) instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.

This time, the sum of the results is \( 2 \times 4 + 8 \times 5 = 48\).

Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?

namespace AoC2024;

public static partial class MullItOver
{
    public static int PartTwo(string filePath)
    {
        return ParseCommands(filePath).Aggregate(
            new RunningSum(true, 0), 
            (res, cmd) => cmd switch {
                DoCommand doCmd => new(true, res.Sum),
                DontCommand dontCmd => new(false, res.Sum),
                MultiplyCommand multCmd => new(
                    res.Enabled,
                    res.Sum + (res.Enabled ? (multCmd.Num1 * multCmd.Num2) : 0)),
                _ => throw new ArgumentException($"Unrecognized type {cmd.GetType()}")
            },
            res => res.Sum);
    }

    private readonly record struct RunningSum(bool Enabled, int Sum);
}