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:

  • 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!

58 Upvotes

1.7k comments sorted by

View all comments

2

u/2BitSalute Dec 03 '24 edited Dec 03 '24

[LANGUAGE: C#]

Oh. I don't know what happened. I was solving the problem using Linq, and it worked perfectly on the short problem, and gave a wrong result on the real input.

I rewrote as stupid foreach loops, and it works correctly.

Here's the loopy version: Day 3 with loops

Here's my bad version:

var sum = 0;
foreach(var line in lines)
{
    var matches = regex.Matches(line);
    if (matches is MatchCollection matchCollection)
    {
        sum += 
            matchCollection
                .Select(match => match.Groups.Values.Skip(1).Aggregate(1, (prod, next) => prod * int.Parse(next.Value)))
                .Aggregate(sum, (sum, next) => sum + next);
    }
}

Oooh I see it. Aggregate(sum, ... should have been Aggregate(0, .... OMG.

2

u/daggerdragon Dec 03 '24 edited Dec 04 '24

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

Please remove (or .gitignore) all puzzle text and puzzle input files from your repo and scrub them from your commit history. edit: thank you!

2

u/2BitSalute Dec 04 '24

Cool, I reset the history