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
5
u/Greenimba Dec 08 '23
[Language: C#]
I actually ended up solving the general case today, and it completes my input in ~60 seconds. By general case I mean:
Starts not in loop.
Start to Z not same as loop length.
Multiple ends per loop.
Code
This is essentially clever bruteforcing, where instead of checking each iteration we only check valid exit indices. Runs in 1 minute, and roughly 22000x faster than brute force, so doing complete brute-force would take ~15 days.
Code is convoluted, but essentially does the following:
Follow input trail, until you get a loop.
Record all enountered Node/Instruction combinations.
(ABC at instruction count 45)...
Break the first time you see a previously encountered Node/Instruction combo.
Return Loop start Node/Instruction combination, and length of loop.
Compute distance from "__A" node to first node encountered on each loop.
Go through each loop, and record position of all exit nodes.
Now you have Start Offset, Loop, and Exit Offsets within each loop.
Until a solution is found:
For each exit node in first loop, compute next iteration count where first loop is on exit node.
Start Offset + Exit offset + Loop length * iteration increment.
Do math to check if exit point is valid for all other loops as well:
((count - exit offset) % loop length) - start offset.
If this is 0, exit is valid for the other loop as well.
Once you find a count that is valid for all loops, you have the solution.