🎄 Advent of code 2025 in F# - day 03

In the video, the presenter solves Day 3 of Advent of Code 2025 in F#, starting with a brute force method to find the maximum joltage from pairs of batteries and then advancing to a dynamic programming recursive solution for selecting 12 batteries efficiently. They emphasize the importance of breaking down complex problems, debugging carefully, and share insights on improving performance and code clarity throughout the challenge.

In this video, the presenter tackles Day 3 of the Advent of Code 2025 challenge using F#. The problem involves a grid of batteries, each labeled with a joltage value from 1 to 9. Each line of input represents a bank of batteries, and the task is to turn on exactly two batteries per bank. The joltage produced by a bank is the number formed by concatenating the labels of the two selected batteries. The first part of the challenge is to find the largest possible joltage each bank can produce by selecting two batteries.

The presenter begins by parsing the input into lists of integers representing each bank. To find the maximum joltage for each bank, they implement a brute force approach using nested loops to generate all pairs of batteries without duplicates. They then concatenate the labels of each pair, convert them to integers, and find the maximum value. This approach works well for the first part and the example inputs, producing the correct results and summing the maximum joltage values across all banks.

For the second part of the challenge, the problem becomes more complex, requiring the selection of 12 batteries from each bank. The presenter initially tries brute forcing but quickly realizes it is inefficient for larger inputs. They decide to use a dynamic programming approach with recursion to solve the problem more efficiently. The key insight is that the first battery selected must come from a limited window of candidates, and the problem can be subdivided into smaller subproblems by recursively selecting batteries and reducing the problem size.

The recursive solution involves selecting the maximum battery value from the allowed candidates, then recursively solving for the remaining batteries in the bank. The presenter carefully implements this approach in F#, handling base cases and ensuring the recursion is tail-recursive for better performance. They debug the solution using print statements and a debugger, fixing off-by-one errors and verifying that the recursive selection produces the correct sequence of batteries. The final solution efficiently computes the maximum joltage sum for the input banks.

In conclusion, the presenter reflects on the learning experience, noting that the initial brute force approach was a useful starting point but not scalable. The dynamic programming solution provided a much faster and cleaner way to solve the problem. They also share insights about debugging F# scripts and the importance of breaking down complex problems into smaller, verifiable steps rather than attempting large leaps in code. The video ends with an invitation for viewers to share their own solutions and approaches to the Advent of Code challenges in F#.