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!
Quick link to Topaz's
pasteif you need it for longer code blocks
2
u/clouddjr Dec 22 '23 edited Dec 22 '23
[LANGUAGE: Kotlin]
I represent each brick with ranges of values:
For part 1, I first sort the bricks by their z coordinates. This makes it possible to simulate falling one by one. Then, I have a map that for each of the point (x,y) stores the currently biggest z value and id of the brick with that value. So it's a
Map<Point2D, Pair<Int, Int>>. Lastly, for each brick, I find the maximum z for each of the point this brick occupies and put that brick immediately above. While doing this, I also update two other maps:supports: Map<Int, Set<Int>>- stores information about other bricks that this brick supports.supported: Map<Int, Set<Int>>- stores information about other bricks that support this brick.These two maps make it easy to find the answer for part 1 (and later for part 2):
For part 2, it's a simple BFS while keeping information about bricks that have already fallen down. In short, to check if a given brick should fall, I check if all the bricks supporting that brick have fallen already. If yes, this bricks falls as well.
Solution