In essense, this feels like... integrals? Feels like a form of integrals, basically finding the slope of a line, finding the slope of that line, and so on. But they lay out the exact steps needed for this, so it doesn't seem too bad at least?
// Step 1: Add lines until all are 0
bool allZero = false;
while (!allZero)
{
allZero = true;
vector<int> lastLine = readings.back();
vector<int> newLine;
for (int i = 1; i < lastLine.size(); i++)
{
int delta = lastLine[i] - lastLine[i-1];
newLine.push_back(delta);
if (delta != 0) allZero = false;
}
readings.push_back(newLine);
}
This finds all of the deltas for the line, then the deltas for that line, etc., until there is a line with all zeros. That wasn't bad. Now to just work backwards...
// Step 2: Work backwards to add the new values
for (int i = readings.size() - 2; i >= 0; i--)
{
readings[i].push_back(readings[i].back() + readings[i+1].back());
}
Not bad, though all I did was follow the very thorough set of steps put forth in the problem writeup.
This makes sense for a part 2, either go one further beyond, or go one further back. I guess they chose to go one further back.
This one is just exceedingly easy to adapt to this code: simply reverse the initial line just after it's read in!
reverse(readingLine.begin(), readingLine.end());