Advent of Code 2025 - Day 9Movie Theater
| Problem statement | Source code | Tags: GeometryCoordinate compression
Part 1
Just enumerate all pairs of points and find the largest rectangle area.
Part 2
Now we want the rectangle that's inscribed in the polygon. The polygon looks like this:

In case this gives any more intuition π
I'd like to keep the same approach of enumerating pairs of points, but now with an additional check to see if the rectangle ever intersects with the polygon. The most straightforward way is to paint the polygon on a grid and check if any point in the rectangle is not painted; however, since the coordinates span about units, we can't map the coordinates directly to grid indices. Instead, we can use coordinate compression to map the coordinates to a smaller grid, noticing that most grid indices are unnecessary since only the corner coordinates of the polygon are relevant.
I've done this many times. First I create two lookup tables, one decompressor and one compressor. I map coordinates to the range 1..=n, because I want to reserve a rim of padding around the grid for flood-filling the outside.
This is the new shape:

Now ideally I can start from an interior point and flood-fill the inside of the polygon. However, finding an interior point is a bit tricky, so instead I flood-fill the outside of the polygon. I've also done this many times.
Because the polygon has no "holes", any rectangle that intersects with the outside must also intersect with the polygon. So we can just check if any point on the rectangle border is marked as 2.