๐ŸŽ„ Advent of code 2025 in F# - day 07

In this video, the presenter solves Day 7 of Advent of Code 2025 in F# by simulating a beam traveling through a structure with splitters, using recursion and sets to accurately count splits and handle overlapping beams. For Part Two, they model quantum branching paths with an optimized breadth-first search that merges identical beam heads to efficiently count distinct timelines, demonstrating effective functional programming techniques despite initial challenges.

In this video, the presenter tackles Day 7 of Advent of Code 2025 using F#. The problem involves simulating a beam traveling down a structure resembling a Christmas tree, where the beam splits every time it hits a splitter. The main challenge is to calculate how many splits occur, especially considering edge cases where multiple splits converge at the same position, which affects the total count of beams and splits. The presenter initially explores whether simply counting splitters is sufficient but realizes that not all splitters are hit by the beam, so a full simulation is necessary.

The presenter begins by parsing the input into a suitable data structure, extracting the starting beam location and the set of splitter locations. They use F# sequences and sets to efficiently represent and manipulate these positions. The parsing step also determines the depth of the structure, which is essential for controlling the simulationโ€™s recursion. With the input parsed, the presenter implements a recursive simulation function that moves the beam down one row at a time, splitting beams when they encounter splitters and avoiding duplicates by using sets.

For Part One, the simulation counts the total number of splits by comparing the number of beams before and after each split. This approach successfully handles the problemโ€™s nuances, including overlapping beams and splitters that are not hit. The presenter confirms the solution works on the example input and then applies it to the actual puzzle input, achieving the correct result efficiently. This part of the solution demonstrates a clear and effective use of recursion and state management in F#.

Part Two introduces a more complex scenario involving quantum mechanics, where a single particle takes both paths at each splitter, effectively creating multiple timelines. The problem requires counting the total number of distinct timelines after the particle completes all possible journeys. The presenter models this as a breadth-first search, tracking entire paths rather than just beam heads. Initially, this approach leads to performance issues due to the exponential growth of paths, but the presenter optimizes by merging paths that share the same head, summing their counts to avoid redundant computations.

In conclusion, the presenter reflects on the solutionโ€™s development, noting the challenges of managing types and state in F#, especially when extending the simulation for Part Two. Despite some initial confusion and a lack of formal testing, the final solution is both elegant and efficient. The video highlights the power of functional programming for solving complex recursive problems and encourages viewers to share alternative approaches or optimizations. The presenter looks forward to continuing the Advent of Code series in F#.