r/adventofcode • u/daggerdragon • Dec 06 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-
--- Day 6: Chronal Coordinates ---
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 6
Transcript:
Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 0:26:52!
32
Upvotes
View all comments
1
u/GeneralYouri Dec 06 '18 edited Dec 07 '18
Unfortunately I wasn't able to solve this one as soon as it went live, because looking at my time just now it could very well have been my first leaderboard entry.
When I read the description I got a bit worried about the infinity stuff, but it turns out you can almost entirely ignore it. After taking a quick peek at my input, my lowest coordinate was 40 and the highest was 359, so my grid ran from 0 to 400.
NodeJS/JavaScript
Part 1:
Part 2:
I kept the algorithm simple and low level (which often seems the fastest choice with these puzzles), and simply iterated the grid to see for each point which of the input coords is closest. I separately kept track of the lowest distance, and the coord it belongs to. This made the infinity aspect easy to solve by checking for x/y equal to 0 or 399, in which case I kept the lowest distance but nullified the coord it belonged to. I also recorded all of these infinite regions, so I could later exclude them when finding the largest region.
As you can see from the code length, part 2 turned out to be easier than part 1. I actually ran it twice, the second time with my grid expanded to -1000 to 1400, but got the same result there. The code isn't at all refactored or optimized, not even generic, this is exactly the code that gave me my answers (and I'm pretty proud of how clean today turned out, especially compared to some of my other days!).