Advent of Code F# – Day 24

Yan Cui

I help clients go faster for less using serverless technologies.

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.

 

The description for the challenge is quite long and wordy, but ultimately there are only three things we need to remember:

  1. the total weight of packages need to be evenly divided into 3 groups
  2. the first group needs to have fewest packages possible
  3. given the same number of packages (fewest possible), the quantum entanglement (i.e. the product…) of the weights of packages in the first group needs to be the smallest possible

Based on these information, we can have the following:

day24_01

For my input, all the weights are odd numbers, and the average weight is an even number. Which means we need an even number of packages in each group, furthermore, based on the input the smallest number of packages that can add up to the average weight is 6.

So rather than a brute force approach (which I tried and failed with) we can be smarter and:

  1. only consider the first group (since that’s all we need to answer the question)
  2. only consider groups of length 6

So, for each package, we will:

  • yield the 6-package groups that can be formed with it
  • yield the 6-package groups that can be formed without it

and we’ll short circuit a path if after collecting 6 packages in a group but the total weight of the group doesn’t add up to the average.

This is exactly what the following code snippet does, it returns all the 6-package groups that has a total weight equalling the average weight:

day24_02

Finally, we just need to find the smallest quantum entanglement value of all the possible combinations:

day24_03

 

Part 2

Same approach as above, but slightly different setup.

day24_04

Now that we’re dividing the total weight of all the packages by 4 instead of 3, the average is now an even number – meaning we’ll need an odd number of packages per group. Also, the smallest number of packages per group is now 5:

day24_05


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.
  2. Consulting: If you want to improve feature velocity, reduce costs, and make your systems more scalable, secure, and resilient, then let’s work together and make it happen.
  3. Join my FREE Community on Skool, where you can ask for help, share your success stories and hang out with me and other like-minded people without all the negativity from social media.

 

Leave a Comment

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