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/GassaFM Dec 22 '23
[LANGUAGE: D] 547/483
Code: part 1, part 2.
Inefficient implementations today: dropping the bricks works for a couple seconds, and testing the chain disintegrations takes a couple dozen seconds.
To drop the bricks, one iteration sorts them by z-coordinate and then, in that order, drops each brick by 1 unit for as long as possible. The iterations go for as long as the state changes after a single iteration.
For Part 1, I remove each brick (by subtracting 1000 from its upper z-coordinate) and check all bricks that lie one layer above it. We need only the first positive check to happen.
For Part 2, in the above procedure, I also remove each brick that gave a positive check, and continue checking.
Here, I'd like to showcase a feature of D language which is helpful in managing complex state during a search: scope guards. Consider the main loop for Part 1 (somewhat cramped up vertically for the post):
The statement
b[i].z2 -= mucheffectively removes the i-th brick from consideration. The next statement isscope (exit) {b[i].z2 += much;}. This means "when we exit the current{}scope for whatever reason, restore the i-th brick". Writing them together helps to not forget to do it, which is something.But wait, there's more! The next such pair starts with
b[j].z1 -= 1;, which means "lower the j-th brick down a notch to see what happens". There, the corresponding restore statement isscope (exit) {b[j].z1 += 1;}. Here, the effect is that it happens even when we exit the loop abruptly viabreak.Multiple
scopestatements in the same scope execute in reverse order.