r/adventofcode • u/daggerdragon • Dec 08 '23
SOLUTION MEGATHREAD -❄️- 2023 Day 8 Solutions -❄️-
THE USUAL REMINDERS
All of our rules, FAQs, resources, etc. are in our community wiki.
Outstanding moderator challenges:
Community fun event 2023: ALLEZ CUISINE!
Submissions megathread is now unlocked!
14 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!
AoC Community Fun 2023: ALLEZ CUISINE!
Today's theme ingredient is… *whips off cloth covering and gestures grandly*
International Ingredients
A little je ne sais quoi keeps the mystery alive. Try something new and delight us with it!
Code in a foreign language
Written or programming, up to you!
If you don’t know any, Swedish Chef or even pig latin will do
Test your language’s support for Unicode and/or emojis
Visualizationsusing Unicode and/or emojis are always lovely to see
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 8: Haunted Wasteland ---
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/thousandsongs Dec 08 '23 edited Dec 08 '23
[LANGUAGE: Haskell]
This was so nice! Both the problem, and Haskell, I think it really shined here.
Parsing is a breeze:
And we end up with a data structure that basically mirrors the input. Now, p1 again is a straightforward following of the problem instructions and translating it into Haskell code:
When I saw p2, I thought I'll just run this same thing, but in parallel for all nodes. Was easy to change the code to do that, and I ran it, but ... crickets ... nothing.
So then I spent a bunch of time debugging my code by inserting Debug.trace commands, to ensure I'm not doing anything wrong. But everything looked okay. Which was my signal to realize that I need to use some shortcut instead of just simulating the entire thing.
Luckily, I soon had my subconscious throw the word "lcm" at me. I still cannot fully justify why taking the lcm of the individual paths is always the correct approach (I guess I could come up with an explanation if I tried, just that nothing clicks immediately), but I knew that's what Eric wanted here. So I just refactored the code to compute individual path lengths
and then return their lcm
A nice thing about this is that it also can be reused to reduce p1 to a single expression.
Link to full solution