Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.
ps. look out for all my other solutions for Advent of Code challenges here.
See details of the challenge here.
Let’s start by capturing the model and input data in a module. Since I’m armed with the hindsight of having seen both parts of today’s challenge I’ve refactored the common logic into a solve function:
- there’s a keypad
- after each move (U, D, L or R) we need to check if the new position is a button, and ignore it if not
- capture the button we finished at at the end of each line
The interesting thing here is the no doubt the solve function.
After we parse the input string we end up with a Direction, which we’ll scan over to get the positions on the keypad we end up at after each line of the input.
However, Array.scan includes the initial value we passed in, which we can solve with Seq.skip 1.
The rest, is just a case of stiching together the actual values from the keypad.
Now that we have the core flow of the solution in place, let’s fill in the bits we need to solve part 1:
You finally arrive at the bathroom (it’s a several minute walk from the lobby so visitors can behold the many fancy conference rooms and water coolers on this floor) and go to punch in the code. Much to your bladder’s dismay, the keypad is not at all like you imagined it. Instead, you are confronted with the result of hundreds of man-hours of bathroom-keypad-design meetings:
1 2 3 4 5 6 7 8 9 A B C D
You still start at “5” and stop when you’re at an edge, but given the same instructions as above, the outcome is very different:
- You start at “5” and don’t move at all (up and left are both edges), ending at
- Continuing from “5”, you move right twice and down three times (through “6”, “7”, “B”, “D”, “D”), ending at
- Then, from “D”, you move five more times (through “D”, “B”, “C”, “C”, “B”), ending at
- Finally, after five more moves, you end at
So, given the actual keypad layout, the code would be
Using the same instructions in your puzzle input, what is the correct bathroom code?
We can present the keypad in part 2 as a Option<char>.
The only other difference to part 1 is that we need to deal with the empty spaces on the keypad, hereby represented as None.
With these two pieces, part 2 is pretty straight forward too.
- Day 2 challenge description
- Advent of Code 2015
- Solution for Day 1
- 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