Advent of Code F# – Day 13

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Hookdeck: The Serverless Event Gateway

Hookdeck is a reliable and scalable serverless event gateway for sending, receiving, authenticating, transforming, filtering, and routing events between services in your event-driven architecture.

Learn more

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


Day 13

See details of the challenge here.

Today’s challenge involves solving two separate problems:

  • counting set bits in an integer
  • level order tree traversal

For the first problem, I recommend reading through this blog post which covers several approaches and optimizations for this well defined problem.

I experimented with 3 of the solutions described in the post, and the results were telling. I skipped the lookup table version because I don’t feel it makes sense in the context of this AOC challenge.


I recently posted about Level Order Tree Traversal in F# recently, which I also used to solve Day 11. We can apply the same technique and optimization (not counting a previously visited coordinate) here and use Seq.unfold to return an infinite sequence of move count with all the new coordinates that are visited in that move.

Another optimization we can apply is to use memoization to avoid computing if a coordinate is an open space more than once.

To solve part 1 we need to find the first round of coordinates that contains our target.


Part 2

How many locations (distinct x,y coordinates, including your starting location)
can you reach in at most 50 steps?

Since we’ve done most of the hard work with the travel function (which returns only the distinct coordinates), the hardest thing about part 2 is to remember to add 1 to account for the starting position (which I forgot to initially of course )



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.

1 thought on “Advent of Code F# – Day 13”

  1. Pingback: Advent of Code F# – Day 14 |

Leave a Comment

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