Advent of Code F# – Day 16

(this post is part of the F# Advent Cal­en­dar project, check out all the oth­er great posts there! And spe­cial thanks to Sergey Tihon for orga­niz­ing this!)

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.


Day 16

The input for today’s chal­lenge looks like this:

Sue 1: gold­fish: 6, trees: 9, aki­tas: 0

Sue 2: gold­fish: 7, trees: 1, aki­tas: 0

Sue 3: cars: 10, aki­tas: 6, per­fumes: 7

Sue 4: per­fumes: 2, vizs­las: 0, cars: 6

so to make pars­ing a lit­tle eas­i­er, I decid­ed to use Regex with a bit of help from an active pat­tern:


and we also need a type to rep­re­sent what we remem­ber about each Aunt Sue. (ok, we don’t strict­ly need one, a tuple can also do the trick, but as we’ll be pass­ing the pay­load around I think it’s a light­weight record would be eas­i­er to work with in this case )


For each line in the input:

Sue 1: gold­fish: 6, trees: 9, aki­tas: 0

we need to fetch the num­ber, and a key-val­ue pair for each of the things we remem­ber about that par­tic­u­lar Aunt Sue. Some­thing along the lines of:


Now, we have all the infor­ma­tion we can remem­ber about all 500 Aunt Sue’s, as well as the facts our My First Crime Scene Analy­sis Machine has told us about the Aunt Sue that we need to send a thank you card for:


Final­ly, we just need to go through all the Aunt Sue’s, and find the one that match­es up with the facts.

As the require­ment spec­i­fied,

Things miss­ing from your list aren’t zero — you sim­ply don’t remem­ber the val­ue.

so if we can’t find a giv­en key (from the list of facts) in the things we remem­ber about an Aunt Sue we should just pre­sume it to be true:



Day 16 (Part 2)

Oh, what shock hor­ror, that we man­aged to mis­read the machine’s out­put! Luck­i­ly, our code is writ­ten in F# and pat­tern match­ing makes the required change a piece of cake 

Since the new require­ments are:

the cats and trees read­ings indi­cates that there are greater than that many, while the pomera­ni­ans and gold­fish read­ings indi­cate that there are few­er than that many…

so we just need to tweak our log­ic slight­ly with addi­tion­al match cas­es to cater for those two spe­cial cas­es:


and job done! F# saves the day and Aunt Sue gets her thank you card! ho ho ho