Advent of Code F# – Day 15

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.


First, let’s cap­ture what an ingre­di­ent is in our mod­el:


Notice that we’ve also added the + and * oper­a­tors to make com­bin­ing dif­fer­ent ingre­di­ents and mul­ti­ply­ing by quan­ti­ty eas­i­er when we mix them togeth­er to make and score a cake:


The input for this chal­lenge looks like the fol­low­ing:

Sprin­kles: capac­i­ty 2, dura­bil­i­ty 0, fla­vor -2, tex­ture 0, calo­ries 3

But­ter­scotch: capac­i­ty 0, dura­bil­i­ty 5, fla­vor -3, tex­ture 0, calo­ries 3

Choco­late: capac­i­ty 0, dura­bil­i­ty 0, fla­vor 5, tex­ture -1, calo­ries 8

Can­dy: capac­i­ty 0, dura­bil­i­ty -1, fla­vor 0, tex­ture 5, calo­ries 8

so let’s parse them into our mod­el:


here I have reused the same Regex active pat­tern as Day 7 to parse each line.

Next, from the list of ingre­di­ents, we need to work out all com­bi­na­tions of them that add to a total of 100 ingre­di­ents being used:


From the above code, accN means the total num­ber of ingre­di­ents accu­mu­lat­ed so far — it saves me from hav­ing to cal­cu­late it each time. For each kind of ingre­di­ents, we may include 0 to 100 of it — 0 = don’t use this ingre­di­ent in the cake; 100 = make the cake out of only this ingre­di­ent. In hind­sight, based on the data, you can’t make a cake with a non-zero score if you have 100 of any ingre­di­ent as each ingre­di­ent has a neg­a­tive val­ue for one of its attrib­ut­es.

Final­ly, for each com­bi­na­tion, make and score the cake and find the high­est score:



Part 2

Not much needs to change here, except we need to fil­ter out cakes whose calo­ries count is not 500: