Advent of Code F# – Day 15

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

I never fully recovered my workspace setup when I upgraded my laptop two years ago, and I still miss things today. If only I had known about Gitpod back then…

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.

 

First, let’s capture what an ingredient is in our model:

day15_01

Notice that we’ve also added the + and * operators to make combining different ingredients and multiplying by quantity easier when we mix them together to make and score a cake:

day15_02

The input for this challenge looks like the following:

Sprinkles: capacity 2, durability 0, flavor -2, texture 0, calories 3

Butterscotch: capacity 0, durability 5, flavor -3, texture 0, calories 3

Chocolate: capacity 0, durability 0, flavor 5, texture -1, calories 8

Candy: capacity 0, durability -1, flavor 0, texture 5, calories 8

so let’s parse them into our model:

day15_03

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

Next, from the list of ingredients, we need to work out all combinations of them that add to a total of 100 ingredients being used:

day15_04

From the above code, accN means the total number of ingredients accumulated so far – it saves me from having to calculate it each time. For each kind of ingredients, we may include 0 to 100 of it – 0 = don’t use this ingredient in the cake; 100 = make the cake out of only this ingredient. In hindsight, based on the data, you can’t make a cake with a non-zero score if you have 100 of any ingredient as each ingredient has a negative value for one of its attributes.

Finally, for each combination, make and score the cake and find the highest score:

day15_05

 

Part 2

Not much needs to change here, except we need to filter out cakes whose calories count is not 500:

day15_06

Whenever you’re ready, here are 3 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. 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.
  3. 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 *