Advent of Code F# – Day 14

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Hookdeck: The Serverless Event Gateway

Hookdeck is a reliable and scalable serverless event gateway for sending, receiving, authenticating, transforming, filtering, and routing events between services in your event-driven architecture.

Learn more

The source code for this post (both Part 1 and Part 2) is available here and you can click here to see my solutions for the other Advent of Code challenges.

Description for today’s challenge is here.


This challenge involves a lot of calculations with km, seconds, km/s, etc. so let’s define these units:


Since a reindeer can be either resting, or flying, let’s capture that as its status:


and finally, let’s capture other important info such as speed, how long a reindeer can fly/rest for, etc:


The input to this challenge looks like this:

Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds.

Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds.

Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds.

so let’s parse each line into a new State for a reindeer:


couple of things to note from the above:

  1. Distance is initialized at 0<km>
  2. Status is initialized as Flying 0<s>, as in ‘has flown for 0 seconds’

Next, let’s simulate how the state of a reindeer transitions from one second to the next:


this should seem pretty straightforward, the only thing worth pointing out is that when Status transitions from Resting => Flying and Flying => Resting, the new status is associated with 1<s> rather than 0<s>. That’s because in the second that has just passed (between the start of the function and the end), we have actually been in the new status.

e.g. at the start of the function, we deemed that we have already flown for the max amount of time possible, so for the duration of this second, we’ll be resting, which is why our Distance hasn’t changed and why by the end of the function, we’ll have rested for 1 second.

Finally, let’s run this simulation for 2503 seconds and find the furthest Distance of all the reindeer:



Part 2

Part 2 introduces a new concept of Score, so let’s update our model:


and at the end of each round of simulation, we’ll need to award the reindeer that has flown the furthest with a point each:


and finally, we’re looking for the highest Score instead of the furthest Distance:


Whenever you’re ready, here are 4 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

Leave a Comment

Your email address will not be published. Required fields are marked *