Advent of Code F# – Day 20

You can become a serverless blackbelt. Enrol to my 4-week online workshop Production-Ready Serverless and gain hands-on experience building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get 15% discount with the code yanprs15!

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

 

Day 20

See details of the challenge here.

Today’s input looks like this:

1873924193-1879728099
2042754084-2076891856
4112318198-4113899685
1039794493-1057170966
3791841434-3797494664
1518668516-1518748952
1946127596-1953926346
4058215215-4086224696
3429681642-3455096313
2599576643-2604275147
1800210010-1801990849
1761160149-1766904471
2774395403-2774748831
1520470679-1542287000
2343327790-2346083217

First, let’s read the input into a list of tuples representing the lower-upper bound ranges.

Notice that I’ve also gone through the trouble of sorting the input by the lower bounds. This way, as we iterate through the ranges we can build up an overal range where:

  • the lower bound is 0
  • the upper bound is the highest upper bound value we have processed so far

and where there are gaps between the current upper bound and the lower bound in the next range, those are the allowed IPs values.

eg. for my input the first 3 ranges are:

(0, 1888888)

(1888889, 1904062)

(1900859, 2087697)

  1. after processing (0, 1888888) the overall range is 0-1888888
  2. (1888889, 1904062) forms a continuous range after 1888888, so the overall range is now 0-1904062
  3. (1900859, 2087697) overlaps with the overall range on the lower bound, that’s ok, the overall range is now 0-2087697

no gaps were found in the 3 ranges so far.

Suppose the next range is (2087700, 2100000), then a gap would appear and we will have 2 allowed IPs: 2087698 and 2087699.

To put that into code, here’s a findAllowedIPs function that will process all the sorted list of ranges and yield all the allowed IPs as a lazy sequence. ps. for part 2 where we need to find the no. of allowed IPs this is a O(n) solution where n is the no. of blacklist ranges.

To solve part 1, we need the first allowed IP.

let part1 = findAllowedIPs input |> Seq.head

 

Part 2

How many IPs are allowed by the blacklist?

let part2 = findAllowedIPs input |> Seq.length

 

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 I help companies go faster for less by adopting serverless technologies successfully.

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.


Skill up your serverless game with this hands-on workshop.

My 4-week Production-Ready Serverless online workshop is back!

This course takes you through building a production-ready serverless web application from testing, deployment, security, all the way through to observability. The motivation for this course is to give you hands-on experience building something with serverless technologies while giving you a broader view of the challenges you will face as the architecture matures and expands.

We will start at the basics and give you a firm introduction to Lambda and all the relevant concepts and service features (including the latest announcements in 2020). And then gradually ramping up and cover a wide array of topics such as API security, testing strategies, CI/CD, secret management, and operational best practices for monitoring and troubleshooting.

If you enrol now you can also get 15% OFF with the promo code “yanprs15”.

Enrol now and SAVE 15%.


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 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!


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!