r/adventofcode • u/Kn0wnAHG0RI • Dec 09 '25
Help/Question Guidance on day 9 part 2
I really want to come up with a solution on my own but i’m not sure if there’s a specific algorithm I don’t know about. Any small hint would be really helpful so I can go learn what i need to and solve it! Thank you
7
Upvotes
18
u/sleekmountaincat Dec 09 '25
Here is some guidance, this is the solution I came up with. I am sure there are fancier/better ways, but breaking it down like this was very logical to me.
1. compress 2d points so you can grid represent in array. this is an awesome trick and i am super happy to have in my toolbag now. Search "compress 2d coordinates", YouTube videos helped me understand it. Basically, create a map of each sorted unique value for x values, and one for y sorted unique values. So lowest x=1, second lowest =2, etc. Then create a new list of points using the mapped values for x and y. Then you have a lossless compressed representation, with size of grid < number of points instead of size of grid being highest x/y values. So cool
2. rasterize the polygon (fill in the edges). This is pretty easy cause we know point1 is connected to point2, etc. don't forget last point is connected to first point.
3. use raycast point in polygon to find a single inside point. You need to be careful implementing this as the grid representation has non zero width boundy. Find first ".", if there are an odd number of ".#" or "#." transitions, that point is inside.
4. Flood fill polygon using the found inside point as starting. Dfs works great
5. Take your part one, but only calculate areas where the rectangle describe by the two corners have all borders as inside the polygon. Easy cause it's been filled!
Mine runs in 70ms!
https://github.com/sleekmountaincat/aoc2025/blob/main/src/day9/q2.ts
6
u/1234abcdcba4321 Dec 09 '25
There really isn't a specific algorithm you need to know about.
Consider the following logic: Given a bunch of lines, a rectangle is definitely not inside the shape formed by those lines if any of those lines intersects with the rectangle.
Now you just need to figure out how to tell if a line intersects with a rectangle, then the rest is a simple three for loops.
More detailed here: https://www.reddit.com/r/adventofcode/comments/1pibab2/2025_day_09_part_2_that_escalated_quickly_in_need/nt4t2bg/
3
u/CarrotOk2548 Dec 09 '25
The way I did it was to decompose the shape of the input into disjoint rectangles.
Then for each candidate rectangle (choosing two corners from the input), I compute the total area that intersects with these rectangles (so the sum of the intersection areas).
If this area equals the area of the rectangle, then it is inside the shape and can be considered.
The tricky part for me was decomposing the input into a set of rectangles. I came up with the following algorithm in the end:
When this is done, we have decomposed the shape into rectangles and we can proceed with the easier part of computing rectangle intersection areas.
I had some bugs because actually higher y coordinates here means going lower but I realized this and got it working in the end
3
u/Sinescape Dec 09 '25
It is quite amazing how many different ways can lead to working solutions.
Mine calculates the edges of a polygon exactly 1 around the given polygon (basically the "edge of the outside"), then checks each rectangle for intersections with any of those edges.
2
u/tobega Dec 09 '25
I did a search and found that there are algorithms for finding largest area axis-parallel rectangles in polygons, but I don't know if that is what is required here. It may be and feel free to read up.
But basically this looks like a pruning problem to me. You are doing a DFS, basically, and need to figure out what criteria you can use to avoid going down a certain branch as early as possible.
Are there certain picks of the other corner that surely must be impossible from the start? Well, the obvious one is if it makes a smaller rectangle than the one you already have. But then what about checking that it doesn't go outside the boundary? How can that be done? Maybe checking if line segments intersect your rectacle?
I havn't solved it yet, but my colleague started with a solution that took well almost two minutes, the got it down to 24s, then to 4s, just by finding better pruning criteria.
2
u/EdgyMathWhiz Dec 09 '25
Aside from the other suggestions, when you're stuck, trying to find a way of visualising what's going on (with the actual problem data) is often a good idea. (I scaled the points down to fit in the range 0,100 and plotted that).
For this problem in particular, this is pretty helpful - when my initial solution was "too small", I was able to pin down "OK, well obviously one of the rectangle corners must be THIS one (that the code had correctly identified), and the other corner looks 'plausible', let's look at what's going on near there". Turns out I could have made it just a little bigger by using a nearby point as the corner instead and I was able to find the exact line segment where my code said "this invalidates the rectangle" and actually it didn't (a < v.s. <= mistake).
2
u/daggerdragon Dec 09 '25
Next time, use our standardized post title format.
Help us help YOU by providing us with more information up front; you will typically get more relevant responses faster.
2
u/mothibault Dec 09 '25
Instead of thinking outside the
boxpolygon, you may want to think inside the rectangle.1
u/AutoModerator Dec 09 '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/The_Cers Dec 09 '25
I have a very bad brute force solution that uses algorithms for intersecting line segments (example)and ray casting (example)to find out if given rectangle wont be intersected by any edge of the input and lies completely within the tiles
1
u/Ill-Rub1120 Dec 09 '25
I tried this but got the wrong answer. At first I thought it might have been more difficult than this where you had to be clever and check concavity. After seeing some visualizations, I realized the shape is not that complex. Ill debug in a bit. Hopefully I find my bug.
1
u/Major_Dog8171 Dec 09 '25
Idk if there is any specific algorithm, but the solution that I came up with is just optimized brute force lol. coordinate compression+dfs+prefix sum.
0
u/[deleted] Dec 09 '25
[deleted] — view deleted comment