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.
I initially approached today’s challenge as a dynamic programming exercise, but it quickly transpired that there’s a much better way to do it once I realised that part 1 is in fact the Josephus Problem and there’s a simple solution to it.
To understand the above, watch the YouTube video in the links section below.
Realizing the folly of their present-exchange rules, the Elves agree to instead
steal presents from the Elf directly across the circle. If two Elves are across
the circle, the one on the left (from the perspective of the stealer) is stolen
from. The other rules remain unchanged: Elves with no presents are removed from
the circle entirely, and the other elves move in slightly to keep the circle
For example, with five Elves (again numbered 1 to 5):
- The Elves sit in a circle; Elf 1 goes first:
1 5 2 4 3
- Elves 3 and 4 are across the circle; Elf 3’s present is stolen, being the one to
the left. Elf 3 leaves the circle, and the rest of the Elves move in:
1 1 5 2 --> 5 2 4 - 4
- Elf 2 steals from the Elf directly across the circle, Elf 5:
1 1 - 2 --> 2 4 4
- Next is Elf 4 who, choosing between Elves 1 and 2, steals from Elf 1:
- 2 2 --> 4 4
- Finally, Elf 2 steals from Elf 4:
2 --> 2 -
So, with five Elves, the Elf that sits starting in position 2 gets all the
With the number of Elves given in your puzzle input, which Elf now gets all the
I’m not aware of this variation to the Josephus Problem, but I’d wager that there would be some pattern to the results similar to part 1, so I put together a dynamic programming solution to get some outputs. (ps. the solution is not good enough for the input as it’ll take too long to return)
With the help of this I can see a pattern emerging:
n : answer
1 : 1
2 : 2
3 : 3
4 : 1
5 : 2
6 : 3
7 : 5
8 : 7
9 : 9
10 : 1
11 : 2
12 : 3
13 : 4
14 : 5
15 : 6
16 : 7
17 : 8
18 : 9
19 : 11
20 : 13
21 : 15
22 : 17
23 : 19
24 : 21
25 : 23
26 : 25
27 : 27
28 : 1
29 : 2
30 : 3
- where n is a power of 3 then the answer is itself
- else n can be expressed as m + l where m is a power of 3
- where l <= m (eg. n = 5 = 3 + 2 where m = 3 and l = 2) then the answer is just l
- else the answer is m + (l – m) * 2 (eg. n = 7 = 3 + 4 where m = 3 and l = 4 and m + (l – m) * 2 = 5)
- Day 19 challenge description
- The Josephus Problem – Numberphile (YouTube)
- Advent of Code 2015
- Solution for Day 18
- 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 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!
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 wrong about serverless and vendor lock-in
- You are thinking about serverless costs all wrong
- Just how expensive is the full AWS SDK?
- Many faced threats to Serverless security
- We can do better than percentile latencies
- 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
- Top 10 Serverless framework best practices