2024 AoC Day 3 – Mull It Over

This is a solution to Advent of Code 2024 day 3, written in Raku.

https://adventofcode.com/2024/day/3

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?

I love an AoC challenge that can be solved using regexes.

use Test;

my $test = 'xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))';
my $input = slurp 'input-3.txt';

sub sum-of-products($input) {
    [+] $input.match(/'mul(' (\d+) ',' (\d+) ')'/,
                     :g)>>.map(+*).map(-> ($a, $b) { $a * $b });
}

is sum-of-products($test), 161, "example input";
say sum-of-products($input);
ok 1 - example input
183669043

Part Two

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

Part 2 can be solved by extending the regex to conditionally match, and to toggle the matching state during regex processing.

use Test;

my $test = Q«xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))»;
my $input = slurp 'input-3.txt';

sub sum-of-products($input) {
    my $matching = True;
    [+] $input.match(/ [ 'mul(' (\d+) ',' (\d+) ')' <?{ $matching }> ]
                     | [ 'do()' <{ $matching = True }> ]
                     | [ 'don\'t()' <{ $matching = False }> ]
                     /,
                     :g)>>.map(+*).map(-> ($a, $b) { $a * $b });
}

is sum-of-products($test), 48, "example input";
say sum-of-products($input);
ok 1 - example input
59097164
raku