Daily bit(e) of C++ | Advent of Code: Day 6
Daily bit(e) of C++ #339, Advent of Code using Modern C++: Day 6.
Welcome to day 6 of Advent of Code using Modern C++. Before you start reading, I highly encourage you to try to solve the two problems yourself: https://adventofcode.com/2023/day/6.
If you missed it, I have prepared a template repository for C++, with a development environment (through a devcontainer) with a bleeding edge version of the C++ toolchain: https://github.com/HappyCerberus/aoc-2023-cpp.
Today, we count the number of ways we can win a toy boat race.
Part one
Today’s problem is straightforward. We can express the distance our boat will travel as hold*(time-hold), and our goal is to find the values of hold such that hold*(time-hold)>distance.
If we reshuffle the variables, we end up with (-1)*hold*hold + hold*time - distance > 0.
We can first calculate the solutions for the quadratic (a*x*x + b*x + c == 0) equation. Once we have the solutions, we can account for the inequality and integer solutions by taking the:
floor of the next lower value from the maximum solution
ceiling of the next higher value from the minimum solution
With the formula ready, we can parse the input and calculate the product across all races.
Part two
Today’s part two is more straightforward than part one. We are solving the same problem with a single race as input.
Conclusion
How did you do with today’s problems? Did you remember the quadratic equation formula?
Share your solutions and insights. Check out the repository with solutions and links to all articles: https://github.com/HappyCerberus/aoc-2023-cpp-solutions.