Advent of Code F# – Day 2

Yan Cui

I help clients go faster for less using serverless technologies.

Table of Content

This article is brought to you by

Is your CI build step taking too long? Try Depot for free today and experience up to 40x faster build speed!

Unlock faster CI for FREE

ps. look out for all my other solutions for Advent of Code challenges here.


Day 2

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:


Part 2

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:

  2 3 4
5 6 7 8 9
  A B C

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 5.
  • Continuing from “5”, you move right twice and down three times (through “6”, “7”, “B”, “D”, “D”), ending at D.
  • Then, from “D”, you move five more times (through “D”, “B”, “C”, “C”, “B”), ending at B.
  • Finally, after five more moves, you end at 3.

So, given the actual keypad layout, the code would be 5DB3.

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.



Whenever you’re ready, here are 4 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

Leave a Comment

Your email address will not be published. Required fields are marked *