Advent of Code F# – Day 11

You can become a serverless blackbelt. Enrol to my 4-week online workshop Production-Ready Serverless and gain hands-on experience building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get 15% discount with the code yanprs15!

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 11

See details of the challenge here.

Today is perhaps the first AOC challenge that is really challenging and required a lot of thought, and I really enjoyed it!

First, let’s model the problem domain.

and we’ll need a way to determine if microchips are fried or we have succeeded in our mission after a move.

 

At its core the problem here is similar to the Level Order Tree Traversal problem we saw yesterday, where from each move you have a no. of follow-up moves you can make:

  • given the floor the elevator lands at, you can take 1 or 2 items from the floor in the next move
  • given the floor the elevator lands at, you can move up or down a floor

For instance, given this state:

F4 .  .  .  .  .  
F3 E  HG HM LG .  
F2 .  .  .  .  .  
F1 .  .  .  .  LM 

Here are the 6 possible combinations of items that can be loaded onto the elevator for the next move:

  • HG
  • HM
  • LG
  • HG, HM
  • HG, LG
  • HM, LG

additionally, you can also move the elevator up or down, leading to a total of 12 possible moves from this point.

The trick, however, is to ensure you don’t make a move that will results in a state that you had already been previously. This optimization alone is sufficient for you to solve part 1 of the challenge in under a minute, but it wasn’t quite enough for part 2…

The key realization for the second optimization is that, the elements themselves don’t matter, ie. all three of these configurations represent the same problem:

F4 .  .  .  .  .  
F3 .  .  .  LG .  
F2 .  HG .  .  .  
F1 E  .  HM .  LM 
F4 .  .  .  .  .  
F3 .  .  .  HG .  
F2 .  LG .  .  .  
F1 E  .  LM .  HM 
F4 .  .  .  .  .  
F3 .  HG .  .  .  
F2 .  .  .  LG .  
F1 E  .  LM .  HM 

because the essence of the problem have not changed:

  • the elevators is at floor 1
  • there’s an element whose Generator is on floor 3 and microchip is on floor 1
  • there’s an element whose Generator is on floor 2 and microchip is on floor 1

So if we can represent states in this way then we can identify equivalent states and avoid repeating them.

With that in mind, let’s override the State type’s ToString method so that a state like this

F4 .  .  .  .  .  
F3 E  HG HM LG .  
F2 .  .  .  .  .  
F1 .  .  .  .  LM 

is represented as “3-(3, 1)(3, 3)” where the tuples represents the (generator floor, microchip floor) for each element and the ordering is determined only by the floor values.

So now, we can drill into the meat of the solution.

There are couple of things to note here:

  • I’m using a mutable HashSet instead of F#’s immutable Set for performance reasons, but not exposing that mutability to the outside
  • itemCombos function returns all possible combinations of items that can be loaded onto the elevator, it’s doing extra work to generate duplicates (and then filtering them out with Seq.distinct) but can be easily rectified away
  • for every combination of items, we generate 1 or 2 new states depending on the ways the elevator can move
  • each new state is added to the cache using its string representation above
  • those states that lead to fried microchips are ignored and not used to generate even more states

 

The solution to part 1 looks like this:

 

Part 2

You step into the cleanroom separating the lobby from the isolated area and put
on the hazmat suit.

Upon entering the isolated containment area, however, you notice some extra
parts on the first floor that weren’t listed on the record outside:

An elerium generator.
An elerium-compatible microchip.
A dilithium generator.
A dilithium-compatible microchip.
These work just like the other generators and microchips. You’ll have to get
them up to assembly as well.

What is the minimum number of steps required to bring all of the objects,
including these four new ones, to the fourth floor?

With both optimizations in place, part 1 took 1s and part 2 took 8s on my MBP, pretty sweet! 

 

Links

 

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter


Hi, I’m Yan. I’m an AWS Serverless Hero and I help companies go faster for less by adopting serverless technologies successfully.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.


Skill up your serverless game with this hands-on workshop.

My 4-week Production-Ready Serverless online workshop is back!

This course takes you through building a production-ready serverless web application from testing, deployment, security, all the way through to observability. The motivation for this course is to give you hands-on experience building something with serverless technologies while giving you a broader view of the challenges you will face as the architecture matures and expands.

We will start at the basics and give you a firm introduction to Lambda and all the relevant concepts and service features (including the latest announcements in 2020). And then gradually ramping up and cover a wide array of topics such as API security, testing strategies, CI/CD, secret management, and operational best practices for monitoring and troubleshooting.

If you enrol now you can also get 15% OFF with the promo code “yanprs15”.

Enrol now and SAVE 15%.


Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.


Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!


Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!