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.
See details of the challenge here.
Today’s input looks like this:
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:
- after processing (0, 1888888) the overall range is 0-1888888
- (1888889, 1904062) forms a continuous range after 1888888, so the overall range is now 0-1904062
- (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
How many IPs are allowed by the blacklist?
let part2 = findAllowedIPs input |> Seq.length
- Day 20 challenge description
- Advent of Code 2015
- Solution for Day 19
- All my F# solutions for Advent of Code
- Github repo
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.
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. Enrol now and enjoy a special preorder price of £9.99 (~$13).
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!
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong