SPRGB - Some (Pseudo) Random Guy's Blog

Advent of Code 2025 Day 9

Problem description

Paraphrasing the problem to more formal terms: given a list of contiguous vertices (x, y), where x and y are non-negative integers, of a simple rectilinear polygon, find the area of the largest rectangle contained inside the polygon where 2 of the opposite corners are also vertices of the polygon.

Observations

Solution

The first part is trivial, which is to generate all possible candidate rectangles sorted by area in descending order. The second part is trickier, because you have to determine whether the rectangle formed is fully contained inside the polygon.

The “eureka moment” is to realize that you don’t have to check that all the points are inside the candidate rectangle, but rather that no side of the polygon fully or partially crosses the candidate rectangle. The sides of the polygon are every pair of two contiguous vertices from the input, plus the pair formed by the first and last vertex to close the loop. There are two cases to determine whether a side crosses the rectangle:

You can find the full code here