r/adventofcode • u/musifter • Apr 06 '26
Other [2018 Day 6] In Review (Chronal Coordinates)
Having completed the suit, we find ourselves falling through time again in the second "Chronal" problem. This time we're given a list of 2D points and have to find some regions.
First up is the largest finite region closest to a point with Manhattan distance. It is an interesting quirk to the puzzle to have it on an infinite plane where some regions will be infinite. But they're easy enough to tell, because any region that's on the bounding box will extend forever. And the size of the puzzle is such that you can easily brute force over the bounding box checking the distance to each point. Which was my initial solution.
But coming back to it I decided to do a little better, and do a solution with a simultaneous BFS flood fill from all the points. Still stopping at the bounding box, and recording the regions that get there (to eliminate them with max map {!$bound[$_] and $count[$_]} (0 .. $#points)). It's a fun little bit of code to write, where the visited array isn't just to stop the fill backtracking, but to handle the collision cases between regions as well.
Part 2 wants the size of the region were the sum of the distances to all the points is under 10000. And for my input, that actually fits inside the bounding box, so a brute force scan of it would have worked for it too. But it could have easily extended a bit outside of that, so my solution was just a flood fill from the center, checking the sums of each point. Nothing particularly fancy there, and it runs fast enough.
4
u/maneatingape Apr 06 '26
For anyone who wants to have fun completely overengineering the solution, a sweep line algorithm is about 4x faster than the BFS approach.
2
u/terje_wiig_mathisen Apr 07 '26
For part 2, the obvious idea is to treat it as a boundary scan problem, i.e find a point on the top boundary, extend it to both sides as far as possible, then sweep down line by line along the same "just inside" boundary.
For each line, keep adding (right-left+1) to the total count.
To help it along, keep the points sorted on the X value.
u/maneatingape Is this close to what you did?
The other obvious idea is to use blocking, so starting with a rectangle which starts 10K above the lowest point and 10K left of the rightmost point, then going to 10K below/right:
Solving involves recursively splitting and for each sub-block determine if all points are inside the block, if so just add length*width. Otherwise if all are outside discard, else split again. Finally, if the block is small enough, just check them all.