Advent of Code F# – Day 1

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

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: