Description for today’s challenge is here.
This challenge involves a lot of calculations with km, seconds, km/s, etc. so let’s define these units:
Since a reindeer can be either resting, or flying, let’s capture that as its status:
and finally, let’s capture other important info such as speed, how long a reindeer can fly/rest for, etc:
The input to this challenge looks like this:
Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds.
Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds.
Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds.
so let’s parse each line into a new State for a reindeer:
couple of things to note from the above:
- Distance is initialized at 0<km>
- Status is initialized as Flying 0<s>, as in ‘has flown for 0 seconds’
Next, let’s simulate how the state of a reindeer transitions from one second to the next:
this should seem pretty straightforward, the only thing worth pointing out is that when Status transitions from Resting => Flying and Flying => Resting, the new status is associated with 1<s> rather than 0<s>. That’s because in the second that has just passed (between the start of the function and the end), we have actually been in the new status.
e.g. at the start of the function, we deemed that we have already flown for the max amount of time possible, so for the duration of this second, we’ll be resting, which is why our Distance hasn’t changed and why by the end of the function, we’ll have rested for 1 second.
Finally, let’s run this simulation for 2503 seconds and find the furthest Distance of all the reindeer:
Part 2 introduces a new concept of Score, so let’s update our model:
and at the end of each round of simulation, we’ll need to award the reindeer that has flown the furthest with a point each:
and finally, we’re looking for the highest Score instead of the furthest Distance:
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, Complete Guide to AWS Step Functions. 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, callbacks, nested workflows, 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