r/adventofcode • u/dllu • Dec 09 '25
Visualization [2025 Day 9 Part 2] [Python] Visualization of the polygon and best rectangle
code to generate (and solve): https://gist.github.com/dllu/f8770b967d48d1dfcfc8e2468f7ab97a
I didn't read the problem statement carefully, so my solution works for points in any order. But in fact the problem statement says that they are given in order of appearance along the polygon, so my complex solution is way overkill lol.
5
u/throwaway_the_fourth Dec 09 '25
Whoa, that's a tricky shape!
5
u/youngbull Dec 09 '25
It would have been more tricky if that cutout in the middle became the largest rectangle in a buggy implementation. As it is, you don't have to worry about whether a rectangle is completely outside the polygon.
2
u/xSmallDeadGuyx Dec 09 '25
Yeah I wrote my code without an inside check because I figured the shape wouldn't be mean like that and was pleasantly surprised when I got the right answer first time! Only had to check edge intersections with the inside of the rectangle. I did verify there were no parallel edges in the input too.
4
4
u/ben0x539 Dec 09 '25
I generated an svg and saw the big gap in the middle and added a "if tile 1 is above the gap and tile 2 is below the gap, don't even try to make a rectangle" check b/c i have no idea how to do anything with geometry
4
1
u/niahoo Dec 09 '25
Hello,
I'm not sure I understand this code:
for i in range(len(xss) // 2):
a = [xss[2 * i], y]
b = [xss[2 * i + 1], y]
v_segs.append((a, b))A vertical segment should share the same x coordinate, not the same y coordinate. Is there some inversion on purpose here?
Thank you for the examples! I have the same shape :)
1
3
u/Zefick Dec 09 '25
Now I see why my solution, which has the flaw of considering rectangles that extend completely outside the shape's borders as valid, actually works.
1
u/sibalashi Dec 09 '25
I saw this image before working on it, and thought it's one giant circle and two example rectangles (didn't see the split) :( Then spend time thinking why a naive "points in rectangle" won't work
4
u/Hacker1MC Dec 09 '25
This image alone allows for a solution to be made much more easily than roughing it out against giant vectors. I am very glad my buddy accidentally sent me this while we were racing to the end, because it took me 15x less time to solve from that point than I had already invested into part 2.