First puzzle
In this puzzle, we have a reindeer competition. The reindeer that flies the longest distance in the competition time wins. A reindeer can fly for x amount of time and after that, it needs to rest for y amount of time. The example in the puzzle description is as follows:
- Reindeer1 => Speed: 14 km/s, Fly-time: 10 seconds, Rest-time: 127 seconds.
- Reindeer2 => Speed: 16 km/s, Fly-time: 11 seconds, Rest-time: 162 seconds.
In a 1000 second competition, Reindeer1 flew 1120 km, and Reindeer2 flew 1056 km. In this case, Reindeer1 wins. The solution to the puzzle is the distance the winning reindeer could fly on a 2503 seconds competition.
First, we need to parse the puzzle input or the reindeer “definition”:
A solution could be to fold a collection of seconds from 1 to competitions seconds on the state of every reindeer. On each one-second iteration, we need to calculate the state of each reindeer. A reindeer could be flying or resting. In addition to that, we need to track how long it was in that state and the flying distance. At the end of the fold, we could just find the maximum distance, and that will be our solution:
Second puzzle
We can reuse a lot from the first puzzle code, but we need to have another function to calculate the reindeer progress and another state adding the score to the mix:
You can find this code along with my input and puzzle answers at here.