• Home
  • About
    • 康青旭 - Germán Caggianese photo

      康青旭 - Germán Caggianese

      Refactoring entropy in my Mind

    • Learn More
    • Email
    • Instagram
    • Github
    • Codeberg   Codeberg
  • All Posts
  • Projects
  • Areas
  • Resources

Advent of Code 2025 - Day 9 in Gambit Scheme

22 Jan 2026

Reading time ~9 minutes

“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

  • Gambit Scheme

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:

  1. Corners: All 4 corners must be inside or on the polygon boundary.
  2. Edges: No polygon segment may cross through the interior of the rectangle.

Algorithm

  1. Polygon Construction: Connect input points sequentially $P_0 \to P_1 \to \dots \to P_n \to P_0$.
  2. 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).
  3. 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

Unless stated otherwise, the content of the website is licensed under a Creative Commons Attribution 4.0 International license.

© 2026 Germán Caggianese(康青旭)