ps. look out for all my other solutions for Advent of Code challenges here.
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.
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 )
- Optimizing bit counting
- Day 13 challenge description
- Advent of Code 2015
- Solution for Day 12
- Solution for Day 11
- All my F# solutions for Advent of Code
- Github repo
Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.
I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.
In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.
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