You can become a serverless blackbelt. Enrol in my course Learn you some Lambda best practice for great good! and learn best practices for performance, cost, security, resilience, observability and scalability. By the end of this course, you should be able to make informed decisions on which AWS service to use with Lambda and how to build highly scalable, resilient and cost-efficient serverless applications.
ps. look out for all my other solutions for Advent of Code challenges here.
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
- 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:
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!
- Day 11 challenge description
- Advent of Code 2015
- Solution for Day 10
- Level Order Tree Traversal in F#
- All my F# solutions for Advent of Code
- Github repo
I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
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.
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!
Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Just how expensive is the full AWS SDK?
- Many faced threats to Serverless security
- We can do better than percentile latencies
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong
- Top 10 Serverless framework best practices