Advent of Code F# – Day 3

Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.

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

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 newsletter


Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

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.


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!


Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you