Daily bit(e) of C++ | Advent of Code: Day 9
Daily bit(e) of C++ #342, Advent of Code using Modern C++: Day 9.
Welcome to day 9 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/9.
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 are predicting the next value in a histogram.
Part one
Today's main challenge is to cut through the long description and understand what we are meant to implement.
We process each line in steps, transforming adjacent elements into the step difference (i.e. right-left). We stop when we reach a range of only zeroes.
However, then we need to predict the next value. The prediction process is described in a very convoluted way. A more straightforward description of the process is: accumulate the last element from each processing step.
Part two
Similarly, for part two, we are sticking with the same transformation process.
The big change is that now we care about the first element from each step, and the correct prediction formula is less obvious. However, consider that if we are trying to predict x, with the next value in the same step being y and z is the transformed result, then z == y-x. We can reshuffle this to x == y-z, which allows us to compute x from memorized elements.
Conclusion
How did you do with today’s problems? Did you manage to cut through the long description?
Share your solutions and insights. Check out the repository with solutions and links to all articles: https://github.com/HappyCerberus/aoc-2023-cpp-solutions.