r/adventofcode • • Dec 22 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 22 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • Community fun event 2023: ALLEZ CUISINE!

    • Submissions megathread is now unlocked!
    • 24 HOURS remaining until the submissions deadline TONIGHT (December 22) at 23:59 EST!

AoC Community Fun 2023: ALLEZ CUISINE!

Your final secret ingredient of this Advent of Code season is still… *whips off cloth covering and gestures grandly*

Omakase! (Chef's Choice)

Omakase is an exceptional dining experience that entrusts upon the skills and techniques of a master chef! Craft for us your absolute best showstopper using absolutely any secret ingredient we have revealed for any day of this event!

  • Choose any day's special ingredient and any puzzle released this year so far, then craft a dish around it!
  • Cook, bake, make, decorate, etc. an IRL dish, craft, or artwork inspired by any day's puzzle!

OHTA: Fukui-san?
FUKUI: Go ahead, Ohta.
OHTA: The chefs are asking for clarification as to where to put their completed dishes.
FUKUI: Ah yes, a good question. Once their dish is completed, they should post it in today's megathread with an [ALLEZ CUISINE!] tag as usual. However, they should also mention which day and which secret ingredient they chose to use along with it!
OHTA: Like this? [ALLEZ CUISINE!][Will It Blend?][Day 1] A link to my dish…
DR. HATTORI: You got it, Ohta!
OHTA: Thanks, I'll let the chefs know!

ALLEZ CUISINE!

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


--- Day 22: Sand Slabs ---


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:29:48, megathread unlocked!

19 Upvotes

274 comments sorted by

View all comments

Show parent comment

1

u/Outrageous72 Dec 23 '23

Yeah, thats why I pass through the deleteds list ...

But I rewrote it to use a queue instead of recursion, it is a lot faster now, 1s but still slow compared to other days, which are mostly under 100ms.

static int CountChainReaction(Brick[] bricks)
{
    return bricks.Where(IsNotDisintegratable).Sum(ChainReaction);

    int ChainReaction(Brick brick)
    {
        HashSet<Brick> fallen = [brick];
        var work = new Queue<Brick>();
        work.Enqueue(brick);

        while (work.Count > 0)
        {
            var b = work.Dequeue();

            foreach (var s in b.Supports)
            {
                // skip when still supported by other bricks that stay in place
                if (s.SupportedBy.Except(fallen).Any()) continue;
                work.Enqueue(s);
                fallen.Add(s);
            }
        }

        return fallen.Count - 1;
    }
}

1

u/Outrageous72 Dec 23 '23

Found a major speed up! Now it is 200ms fallen.Add(b) instead of fallen.Add(s) in the loop!

    while (work.Count > 0)
    {
        var b = work.Dequeue();
        fallen.Add(b);

        foreach (var s in b.Supports)
        {
            // skip when still supported by other bricks that stay in place
            if (s.SupportedBy.Except(fallen).Any()) continue;
            work.Enqueue(s);
        }
    }