πŸŽ„ Advent of code 2025 in F# - day 08

In this video, the presenter solves Day 8 of Advent of Code 2025 in F# by modeling the problem as a graph and using a greedy algorithm to connect 3D junction boxes based on precomputed Euclidean distances, carefully handling bugs and optimizing data structures. The solution successfully builds circuits by merging sets of nodes, addresses both parts of the challenge, and highlights the importance of debugging, sorting, and efficient computation in complex graph problems.

In this video, the presenter tackles Day 8 of Advent of Code 2025 using F#. The problem involves connecting a set of junction boxes in 3D space to form circuits, which resembles creating a minimally connected graph with the shortest distances between nodes. The presenter immediately thinks of graph theory and algorithms but notes limited familiarity beyond basic search and Dijkstra’s algorithm. The input consists of a thousand 3D nodes, making the problem computationally intensive. The initial approach is to parse the input points and precompute all Euclidean distances between every pair of nodes, storing these in a lookup table for efficient access.

The presenter writes F# code to generate all unique pairs of junction boxes and calculates the Euclidean distance between them, carefully handling integer overflow by switching to 64-bit integers. The distances are stored in a map with sorted keys to avoid duplication. After verifying the correctness of the distance calculations on a smaller example, the presenter sorts the pairs by distance to process them in ascending order. This setup reduces the problem to iterating over the sorted list of pairs and connecting nodes greedily based on the shortest distances.

To build the circuits, the presenter models the state as a list of sets, where each set represents a circuit containing junction boxes. The recursive function processes the sorted pairs, merging sets when the two nodes belong to different circuits, effectively building larger connected components. The presenter carefully handles edge cases such as avoiding duplicate connections within the same circuit. Testing on the example input confirms the approach works, but initial attempts on the full input yield incorrect results, prompting debugging.

The presenter identifies two key bugs: an off-by-one error in the number of connections processed and forgetting to sort the circuits by size before calculating the product of the sizes of the three largest circuits. Fixing these issues produces the correct answer for part one. The presenter reflects on the challenges of debugging complex graph problems and the importance of sorting and careful indexing. Despite the computational intensity, the solution scales reasonably well for the input size.

For part two, the problem extends to continuing the connections until all junction boxes form a single circuit. The presenter modifies the recursive function to stop when only one circuit remains and returns the product of the x-coordinates of the last connected junction boxes. The final solution works correctly and efficiently, though the presenter notes the runtime is still somewhat slow. The video concludes with reflections on the importance of choosing appropriate data structures, handling large numbers carefully, and the value of perseverance in solving challenging algorithmic problems. The presenter invites viewers to share alternative solutions and looks forward to continuing Advent of Code challenges in F#.