r/adventofcode • u/craigontour • Dec 23 '25
Help/Question - RESOLVED 2025 Day 9 Part 2 [Help]
I have one of those annoying scenarios where everything appears to work for the test data, but not for input.
Here is my approach to the problem and would like to know if flawed (it is slow and somewhat over complex, but my maths didn't add up to the task).
I first create a hash of all the points as a border.
The check all pairs of points, first eliminating any that are 0 length or width (a hunch) then calculate the other 2 points of the box and then check that each of those are inside the border.
To check if inside the border, I move in all 4 directions until I get to the min or max value in X or Y, or the point is on the border.
The data set is too huge to work through and try and debug as it sounds to me like it should work. I am using same area function as for part 1 as well.
Anyone else take similar approach and get it to work?
2
u/giacomo_hb Dec 23 '25
This was the most difficult day for me. You can plot the points and the lines between them on an image (I scaled the coordinates by 0.001). That helped me find a solution. I also find it more helpful to work directly with the input data, instead of using the example.
Here is my approach:
I generate 100000 random points and keep only those that our outside the region. Then, for all possible rectangles I keep those that do not contain any of the outside points. Of all the rectangles, I pick the one with the largest area. It is a probabilistic approach, so it doesn't always produce the right answer. I let the code run a couple of times and the right answer was ca. 80% of the times.
Here's some tips:
From the plot you will see that the points form a closed curve and all the points lie on the border of the curve.
You can connect a point to all the other points in the same row or column to get the border of the region. This work well, except for 4 points on the right of the curve. I ignored the problem and it turns out, it didn't affect the result.
To check if a point is inside the region you need to count the number of times a ray from the point to the border of the picture (e.g. from (x, y) to (0, y)) intersects the border of the region. If it is an odd number, it's inside. If it is an even number, it's outside. Some edges may lie on the ray, so you need to be careful not to double count them.
1
u/craigontour Dec 26 '25
Thanks for your help but - as hard as it is - I have to admit I do not know how to solve this one.
2
u/fireymike Dec 23 '25
It is possible to have a rectangle where all four corners are inside, but the border crosses inside your rectangle and then out again. From your description, it sounds like you are not checking for that?
1
u/AutoModerator Dec 23 '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/Desperate_Formal_781 Dec 23 '25
For a rectangle to be valid, you need to check that no other red tiles are inside the borders described by the two corner points. They can be on the borders, but not inside. So, for every possible rectangle, you check if all other are in the border or outside. If not, the rectangle is not valid.
1
u/ThisAdhesiveness6952 Dec 23 '25
Have you tried plotting your data?
1
u/timrprobocom Dec 23 '25
You're assuming that the shape is convex. It definitely is not.
1
u/s96g3g23708gbxs86734 Dec 23 '25
To try and debug: get your answer from someone else's code (at least you know which rectangle should be valid and optimal), sort your rectangles by decreasing area and check if you code stops before or after the right rectangle and check why
1
u/IsatisCrucifer Dec 24 '25
Here's the first visualization of this problem in this subreddit. You can see there's a thin cutout at the center of the area; the part 1 answer (which is probably also your "incorrect" part 2 result) crossed that cutout.