Advent of Code F# – Day 1

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!

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.


Day 1

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:

  1. the direction to turn – ie L or R
  2. 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:

  1. the current direction we’re moving in – N, W, E or S
  2. 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)!


Part 2

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 4 blocks 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:

  1. group by the positions, and return the first position with count > 1
  2. 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:



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 weekly 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!