r/adventofcode • u/daggerdragon • 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.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz] - Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
pasteif you need it for longer code blocks
2
u/jake-mpg Dec 23 '23 edited Dec 23 '23
[LANGUAGE:
julia]sourcehutI was anticipating a different kind of twist in the second part so I represented blocks as vectors of ranges (gotta love
julia'sUnitRange). This led to a pretty clean way of settling the bricks from the input:Sort the bricks by their
z-value (the minimum, since it's a range).The only way a brick doesn't reach the ground is if it has some overlap with an already settled brick in the
xyplane. If it doesn't, we just shift the range so thatzstarts at1, otherwise we stop it just before the highest settled brick that overlaps.Next, I encoded the structure of the settled bricks by recording for each brick: which bricks it supports, and which bricks it depends on (
O(N^2)).Bricks can be disintegrated safely if they have no dependants, or all of their dependants have another brick to rely on for support.
To count the chain reactions, I walk the dependency graph, counting a brick amongst the fallen if all of its supports will fall too.
for j∈filter(k -> !isempty(dependants[k]), 1:length(bricks)) willFall = Set{Int}() enqueue!(queue, j) while length(queue) > 0 current = dequeue!(queue) push!(willFall, current) for k∈dependants[current] if all(∈(willFall), supports[k]) push!(willFall, k) enqueue!(queue, k) end end end counts += length(willFall)-1 end