Advent of Code F# – Day 14

The source code for this post (both Part 1 and Part 2) is avail­able here and you can click here to see my solu­tions for the oth­er Advent of Code chal­lenges.

Descrip­tion for today’s chal­lenge is here.


This chal­lenge involves a lot of cal­cu­la­tions with km, sec­onds, km/s, etc. so let’s define these units:


Since a rein­deer can be either rest­ing, or fly­ing, let’s cap­ture that as its sta­tus:


and final­ly, let’s cap­ture oth­er impor­tant info such as speed, how long a rein­deer can fly/rest for, etc:


The input to this chal­lenge looks like this:

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

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

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

so let’s parse each line into a new State for a rein­deer:


cou­ple of things to note from the above:

  1. Dis­tance is ini­tial­ized at 0<km>
  2. Sta­tus is ini­tial­ized as Fly­ing 0<s>, as in ‘has flown for 0 sec­onds’

Next, let’s sim­u­late how the state of a rein­deer tran­si­tions from one sec­ond to the next:


this should seem pret­ty straight­for­ward, the only thing worth point­ing out is that when Sta­tus tran­si­tions from Rest­ing => Fly­ing and Fly­ing => Rest­ing, the new sta­tus is asso­ci­at­ed with 1<s> rather than 0<s>. That’s because in the sec­ond that has just passed (between the start of the func­tion and the end), we have actu­al­ly been in the new sta­tus.

e.g. at the start of the func­tion, we deemed that we have already flown for the max amount of time pos­si­ble, so for the dura­tion of this sec­ond, we’ll be rest­ing, which is why our Dis­tance hasn’t changed and why by the end of the func­tion, we’ll have rest­ed for 1 sec­ond.

Final­ly, let’s run this sim­u­la­tion for 2503 sec­onds and find the fur­thest Dis­tance of all the rein­deer:



Part 2

Part 2 intro­duces a new con­cept of Score, so let’s update our mod­el:


and at the end of each round of sim­u­la­tion, we’ll need to award the rein­deer that has flown the fur­thest with a point each:


and final­ly, we’re look­ing for the high­est Score instead of the fur­thest Dis­tance:


Liked this post? Why not support me on Patreon and help me get rid of the ads!