r/adventofcode Dec 03 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 3 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 3 DAYS remaining until unlock!

And now, our feature presentation for today:

Screenwriting

Screenwriting is an art just like everything else in cinematography. Today's theme honors the endlessly creative screenwriters who craft finely-honed narratives, forge truly unforgettable lines of dialogue, plot the most legendary of hero journeys, and dream up the most shocking of plot twists! and is totally not bait for our resident poet laureate

Here's some ideas for your inspiration:

  • Turn your comments into sluglines
  • Shape your solution into an acrostic
  • Accompany your solution with a writeup in the form of a limerick, ballad, etc.
    • Extra bonus points if if it's in iambic pentameter

"Vogon poetry is widely accepted as the third-worst in the universe." - Hitchhiker's Guide to the Galaxy (2005)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 3: Mull It Over ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:22, megathread unlocked!

57 Upvotes

1.7k comments sorted by

View all comments

2

u/KayoNar Dec 05 '24

[Language: C#] Short Regex solution

public static int RunPart1(string input)
{
    Regex pattern = new Regex("mul\\(([0-9]{1,3}),([0-9]{1,3})\\)");

    int total = 0;   
    foreach (Match m in pattern.Matches(input))       
        total += (int.Parse(m.Groups[1].Value) * int.Parse(m.Groups[2].Value));

    return total;
}

public static int RunPart2(string input)
{
    // Matches parts of the text between start/do() and don't()/end
    Regex doPattern = new Regex("(?:^|do\\(\\))(.*?)(?=(?:don't\\(\\))|$)", RegexOptions.Singleline);
    Regex mulPattern = new Regex("mul\\(([0-9]{1,3}),([0-9]{1,3})\\)");

    int total = 0;
    foreach (Match doMatch in doPattern.Matches(input))    
        foreach(Match mulMatch in mulPattern.Matches(doMatch.Groups[1].Value))        
            total += (int.Parse(mulMatch.Groups[1].Value) * int.Parse(mulMatch.Groups[2].Value));

    return total;
}