Module aoc::year2023::day06

source ·
Expand description

§Wait For It

We can solve analytically using the quadratic formula.

  • x is time spent holding the button.
  • t is the duration of the race.
  • d is the record distance.

Then the distance travelled is:

  • x * (t - x)

To beat the record the following conditition must hold:

  • x * (t - x) = d
  • x² - tx +d = 0

The start and end times where we will be the record are given by the roots of the quadratic equation which we can solve using the quadratic formula.

  • (t ± √(t² - 4d)) / 2

Functions§