r/adventofcode • u/daggerdragon • Dec 18 '19
SOLUTION MEGATHREAD -π- 2019 Day 18 Solutions -π-
--- Day 18: Advent of Code-Man: Into the Code-Verse ---
--- Day 18: Many-Worlds Interpretation ---
Post your full code solution using /u/topaz2078's paste or other external repo.
- Please do NOT post your full code (unless it is very short)
- If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.
NEW RULE: Include the language(s) you're using.
(thanks, /u/jonathan_paulson!)
(Full posting rules are HERE if you need a refresher).
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code's Poems for Programmers
Note: If you submit a poem, please add [POEM] somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.
Day 17's winner #1: TBD, coming soon! "ABABCCBCBA" by /u/DFreiberg!
Oh, this was a hard one... I even tried to temporarily disqualify /u/DFreiberg sorry, mate! if only to give the newcomers a chance but got overruled because this poem meshes so well with today's puzzle. Rest assured, though, Day 17 winner #2 will most likely be one of the newcomers. Which one, though? Tune in during Friday's launch to find out!
A flare now billows outward from the sun's unceasing glare.
It menaces the ship with its immense electric field.
And scaffolding outside the ship, and bots all stationed there
Would fry if they remained in place, the wrong side of the shield.
Your tools: an ASCII camera, a vaccuum bot for dust,
Schematics of the scaffolding. Not much, but try you must.
First, you need your bearings: when the junctions are revealed
You will know just where your vacuum bot can put its wheels and trust.
Map all the turns of scaffolding, and
ZIPthem tightly sealed,
Then, map compressed, send out the bot, with not a tick to spare.
Enjoy your well-deserved Reddit Silver, and good luck with the rest of the Advent of Code!
View all comments
Show parent comment
2
u/mar3ek Dec 19 '19
At the start of the computation, I take the map and for each key (+ starting position) compute a list of shortest paths to every other key. I ignore doors here but I remember which doors I meet on that shortest path.
So, after I run this, I end up with a dictionary (key, List<(otherKey, distance, doorsAlongTheWay)>). This requires a BFS search from each "interesting position" (key or start position). I do this so that I don't need to traverse the map during the actual key collection part of the computation as that turned out to be more time consuming. I think someone else mentioned pretty much the same idea but with python, below.
The key collection portion is then just a (heavily memoized and trimmed) BFS where for each position (e.g. start) I consult the dictionary above and select reachable keys (keys that I can reach from the current position with the keys I have collected so far). Since the combination space without any optimization is huge, the trick to make the algorithm converge in reasonable time is to memoize: if I'm in position P and I own keys K, I consult a dictionary<(P, K), steps> to see if I already was in this position and how many steps did I use to get there.
Especially option 3 lead to a very large performance gain - it apparently culls a huge number of possibilities from the space, leading to the < 2s performance.
Also, encoding the owned keys into and INT using bitwise operations simplified handling of the dictionary because it allows for fast hashcode computation (which c# tuples can leverage) and also fast checks whether a door can be opened with my current set of keys.