r/adventofcode • u/daggerdragon • Dec 23 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 23 Solutions -🎄-
--- Day 23: Experimental Emergency Teleportation ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The 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: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 23
Transcript:
It's dangerous to go alone! Take this: ___
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
View all comments
Show parent comment
3
u/fizbin Dec 26 '18
It finds the Manhattan distance to the box from the bot and then compares it to the bot's radius.
Now, how does
dend up containing the Manhattan distance to the box?Let's start with what's probably a straightforward way to compute that distance:
This corresponds to the idea that intuitively, one way to go from bot to box is to first along the
xdirection until that yourxcoordinate is in thexrange of the box, then move along theydirection, etc.Well, we don't need all those variables, and we can pull that into a loop:
Okay, now let's look what's happening inside the loop. If we had a function that was equal to
box_lo - xwhenxis smaller thanbox_loand equal tox - box_hiwhenxis larger thanbox_hiand equal to0whenxis in between those values, then we could replace the calculation by that.Such a function is in fact the shape of what you get when you average two functions of the form
abs(x - some_val), though you need to subtract an amount to get you back to 0. See this graph on desmos. (Click the circle to the left of the fourth equation there to see what the final form does)So now we can replace that inner loop with:
And that's just what I have, except that I broke the
- (boxhigh - boxlow)out into another step and did the// 2bit at the end.