Advent of Code 2023 Day 6
It's once again the best time of the year; Advent of Code! In this blog series I will be taking a look at all the puzzles of the year, and how I approached solving them.
You can find all of my solutions in my git repo.
Day 6: Wait For It
Today we are taking a look at Day 6
We need to find the amount of time to charge our boat to beat the best time. As soon as I read this puzzle, it was clear this was going to be a "solve x" math question.
The distance our boat travels can be expressed as a function of , where is the amount of time we hold the button, and is the duration of the race.
This can then be rearranged to form a quadratic equation:
As you might remember from high-school, quadratic equations can be solved with the quadratic formula:
Plugging our values in, we get two points for , which are the bounds of our winning area. Now, these are floats, so they must be ceil()
and floor()
:ed. There is one edge case where the bounds have no fractions, which would give us the answer to match the current record. The goal is to beat the record, so +1
is added to fix this.
Python
In part 1 we solve the formula for each race and multiply the possibilities together to get our answer. For part 2, it's as simple as stripping out the whitespace and counting the formula once for the large numbers.