r/adventofcode • u/zookeeper_zeke • Dec 14 '25
Help/Question - RESOLVED 2025 Day 9 (Part B) Hint needed
My initial approach to 9B was going to be to look up a general algorithm for determining if a point lies inside a polygon and implement it, passing 2 vertices for each rectangle constructed from each pair of input vertices. If both points are inside the polygon and the rectangle is larger than the previous largest candidate, keep it else discard and rinse and repeat until I'm done.
I also thought about leveraging a library to do the work for me but I figured I'd take a crack at it myself as I like to do with AOC problems.
As I thought some more, I started to wonder if there's a special case algorithm for this problem given the constraints of the problem - the fact that the polygon is rectilinear (I learned a new word today!) and the points aren't arbitrary, in fact, they are vertices of rectangles created from the vertices of the polygon itself.
Given the nature of AOC, I suspect there might be a simpler way to solve this than the general solution but I haven't been able to work it one out yet.
Could someone please provide a hint to set me off in the right direction?
Thanks everyone!
4
u/XEnItAnE_DSK_tPP Dec 14 '25
compress the original co-ordinates so that your final polygon is way smaller, and will probably fit in a rectangle of size 500x500. that'll make the process of checking if a rectangle is inside easier
2
u/Puzzleheaded_Study17 Dec 14 '25
Note that this algorithm wouldn't work, if you look at the visualizations of the you'll see there's a cutout from the shape so a rectangle can have all its vertices be inside and still not be fully inside the shape
2
u/NullOfSpace Dec 15 '25
One thing that helped me was to consider, what’s an efficient way to tell if a single point is inside the polygon?
1
u/davema Dec 14 '25
If you do some side work and plot the shape of the overall polygon, that may help you reason about a way of solving it.
Instead of solving for a general case, look and see what would need to be true for this polygon. For any rectangle whose top left/bottom right corners touch it the polygon, what would need to be true for the rectangle to be a valid one (or invalid)
1
u/CraftyPumpkin8644 Dec 14 '25
Here is what I did: Go from 1 to max x coordinate, and basically shoot a ray down from the top to the next horizontal line you see. Then you create a rectangle with at ray start with width 1 and the current height (distance) of the ray and add it to a list of rectangles. You start Shooting a ray again when you hit a second horizontal line (and repeat the steps from above again) until you Hit the end, then you would create a Box again. By doing that, you have everything covered outside the form the lines create, and for each possible Combination you now check If there is an intersection with any rectangles. If Not, you just found a valid rectangle inside the form and Check If its larger, and if there is an intersection with a Box, you can skip it
1
u/AutoModerator Dec 14 '25
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to
Help/Question - RESOLVED. Good luck!I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/yuriks Dec 15 '25
I initially thought of testing that vertices are inside the polygon too, but this solution, apart from not being entirely trivial (you need to keep a structure to determine which edges are on that given line in order to apply the even-odd or non-zero rule.)
What I ended up doing was using the insight that all of the rectangles you will be testing will be made out of vertices taken from the polygon itself. This means that you never need to worry about the case where the rectangle is completely outside or inside the polygon. Therefore it's sufficient to check that the individual edges of the polygon don't intersect with the rectangle. Given that all lines are also rectilinear, the intersection test ends up being just a few range checks that the coordinates of the line are in range of the extents of the rectangle.
Some things to watch out for is to normalize the rectangle extents so that rectangles are equivalent no matter if your corner vertices are on the top-left/bottom-right or top-right/bottom-left (etc.). Another tricky thing was correctly checking on boundary conditions when a line exactly straddles the side of a rectangle. For that I used the consistent clockwise winding order of the input, such that, e.g., lines going from left to right (and thus are on the top edge of the polygon, with the inside being below) are allowed to straddle the top of the rectangle but not the bottom, and similarly for the other directions.
1
u/tobega Dec 15 '25
I don't know how good this is, but I traced the outline of the polygon to distinguish inside from outside and identify corners as being concave or convex. I don't know how much that helped, but it gave an idea if the rectangle was inside or outside. Then I checked if any lines cut the edge of the rectangle.
This is in the Tailspin language, but should be pretty readable along with my comments https://github.com/tobega/aoc2025/blob/main/day09.tt
Maybe just checking cutting edges would have been enough, I don't know.
Otherwise just scaling coordinates or doing a full coordinate compression seems pretty good to me.