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.
It’s December again, which means two things have started :
unfortunately I was too busy (that is, until the swift and untimely death of Yubl…) to be involved with the F# Advent Calendar this year, but I’m certainly not going to miss out on the Advent of Code.
ps. look out for all my other solutions for Advent of Code challenges here.
See details of the challenge here.
Starting off nice and easy, let’s capture the domain and input data in a module, along with some helper functions.
Here, we have defined an Instruction active pattern for parsing a string (eg. “L72”) into a tuple of:
- the direction to turn – ie L or R
- the no. of blocks to move in that direction
As we follow along the instructions, we also need to track the direction we’re moving in – North, West, East or South. Hence why I added a turn function that will return the new Direction given a tuple of:
- the current direction we’re moving in – N, W, E or S
- which way we’re turning – L or R
Now we’re ready to answer Part 1 of the challenge.
Most of this code should be straight forward, but there is one point I’d like to draw your attention to. Many people don’t realise you can use active patterns outside of match…with, but you can in fact use it inline where an argument should be as I have done in the Array.fold.
Here, instead of accepting an instruction as a string (eg, “L72”) I can use the aforementioned Instruction active pattern to parse it and capture the angle to turn (L or R) and the no. of steps to move along the new direction, all in one go.
Starting at (0, 0), we iteratively (well, using a fold) turn and move until we have exhausted all the instructions in the input data. To calculate how many blocks away the Easter Bunny HQ is we simply have to add the final X and Y coordiantes together, so long as we don’t forget to Math.Abs first (which I totally did at my first attempt)!
Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice.
For example, if your instructions are
R8, R4, R4, R8, the first location you visit twice is
4blocks away, due East.
How many blocks away is the first location you visit twice?
For part 2, we need to know more than just where we ended up at after each instruction, we need to know all the positions we visited. We can modify the move function from part 1 to return all the positions:
You can also achieve the same result with Seq.scan instead.
We can stick to the same structure as part 1 and use a fold to iterate through all the instructions and built up a trail of positions we have visited. However, it felt a little convoluted, so I opted for a slightly different approach:
The follow function turns a string list and returns all the positions that are visited as a sequence.
To find the first position that are visited more than once, 2 approaches jump to mind:
- group by the positions, and return the first position with count > 1
- iterate through the positions with a mutable HashSet, skipWhile the positions is added to the set (remember, HashSet<T>.Add returns true if the item is added), then take the head of the resulting sequence
Whilst it’s less efficient, I opted for the first approach for its simplicity.
So, putting everything together and you have a solution for part 2:
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