Advent of Code F# – Day 3

Yan Cui

I help clients go faster for less using serverless technologies.

Table of Content

This article is brought to you by

Don’t reinvent the patterns. Catalyst gives you consistent APIs for messaging, data, and workflow with key microservice patterns like circuit-breakers and retries for free.

Try the Catalyst beta

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

 

Day 3

See details of the challenge here.

First, let’s capture the input for today’s challenge in a text file, say Day03Input.txt.

Then, let’s create a module to capture the common steps between both parts of the challenge.

There’s really not much to be said about the code snippet above, and the solution for part 1 is also really simple.

 

Part 2

Now that you’ve helpfully marked up their design documents, it occurs to you
that triangles are specified in groups of three vertically. Each set of three
numbers in a column specifies a triangle. Rows are unrelated.

For example, given the following specification, numbers with the same hundreds
digit would be part of the same triangle:

101 301 501
102 302 502
103 303 503
201 401 601
202 402 602
203 403 603

In your puzzle input, and instead reading by columns, how many of the listed
triangles are possible?

You could, transpose the input (ie, a int[][]) so that you end up with something along the lines of:

101  102  103  201  202  203 …

301  302  303  401  402  403 …

501  502  503  601  602  603 …

which will be easier to process, but it feels like more work than I’d like to do.

An alternative approach – the one I went with – is to use Seq.chunkBySize to group row indices into chunks of 3. For each chunk you can flat map (in F#, that’s Seq.collect) it to 3 triangles.

After that, it’s a case of applying the isFilter function we declared earlier on the resulting sequence.

 

Links

Whenever you’re ready, here are 3 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. 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.
  3. 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 *