“You slide down the firepole in the corner of the playground and land in the North Pole base movie theater!”
Github: GCaggianese/AoC-2025/D9
Puzzle Day 9: Movie Theater
Part One
Find the largest rectangle using any two “red tiles” (input coordinates) as opposite corners.
- Input: List of coordinates $(x, y)$.
- Metric: Area includes the boundary tiles. \(Area = (|x_1 - x_2| + 1) \times (|y_1 - y_2| + 1)\)
- Algorithm: Brute-force $O(N^2)$ comparison of all pairs.
Part Two
The red tiles form a sequential path (loop) connected by “green tiles” (rectilinear segments). The valid rectangle must be formed entirely by tiles inside or on the boundary of this polygon.
Constraints
For a rectangle defined by corners $P_i, P_j$ to be valid:
- Corners: All 4 corners must be inside or on the polygon boundary.
- Edges: No polygon segment may cross through the interior of the rectangle.
Algorithm
- Polygon Construction: Connect input points sequentially $P_0 \to P_1 \to \dots \to P_n \to P_0$.
- Ray Casting: Use the even-odd rule to determine if a point is inside the polygon.
- Correction: Points exactly on horizontal/vertical segments are valid (Green).
- Validation: Iterate all pairs $(P_i, P_j)$. If the area is greater than the current max, validate geometric constraints.
Manual Analysis
Verification of vector distances and area for sample points
\[\vec{V}_1, \vec{V}_2, \vec{V}_3\ :\] \[\begin{aligned} \vec{V}_1 &= [3, 1] \\ \vec{V}_2 &= [2, 5] \\ \vec{V}_3 &= [6, 3] \end{aligned}\]Distance Checks:
\[|\vec{V}_1 - \vec{V}_3| = \sqrt{(3-6)^2 + (1-3)^2} = \sqrt{13} \approx 3.606\] \[|\vec{V}_1 - \vec{V}_2| = \sqrt{(3-2)^2 + (1-5)^2} = \sqrt{17} \approx 4.123\] \[|\vec{V}_2 - \vec{V}_3| = \sqrt{(2-6)^2 + (5-3)^2} = \sqrt{16+4} = \sqrt{20} \approx 4.472\]Area Calculation: For the rectangle defined by $\vec{V}_1$ and $\vec{V}_3$:
\[Area = |3-6| \cdot |1-3| = 3 \cdot 2 = 6\]Polygon Trace
The input points form a rectilinear loop. Visualizing the connectivity:
(0,0) . . . . . . . . .
. . . . . . . . . . . .
. . P5----------P4. . .
. . | . . . . . | . . .
. . P6. . . . . P3. . .
. . | . . . . . | . . .
. . P7----------P2. . .
. . . . . . . . | . . .
. . . . . . . . P1--P0.
Implementation in Gambit Scheme
make && ./build/d9