--- Day 1: Increasing ---

December 1

Listening to: Synthwave


This first one looks pretty simple so far. Literally just a list of how many times a number increases from the previous one. A simple loop and reading in data from a file. Super simple.

ifstream inputFile;
inputFile.open("input");

int baseNum, reading, result = 0;

// First measurement is baseline - ignored for the first 'reading'
inputFile >> baseNum;

while (inputFile >> reading) {
    if (reading > baseNum) result++;
    baseNum = reading;
}

cout << "Number of increases: " << result;

Onto the second half!


Fun, this looks to be building off of the previous one. A more complex version of the same problem, using the same data. Having to keep track of rolling windows to keep track of everything.

Since it's tracks of three, it seems like the simplest solution might be to keep track of three sums, add the reading to all three of them, and have a rolling window that actually has the full 'reading'. Basically, filling buckets by the same amount each time there's a reading, then weighing and throwing one out, switching each one.

It works first try! I had to fudge the initial values to balance everything out.

// Start result at -3 to counter first three readings; these don't count
int reading, baseSum = 0, i = 0, result = -3;
int buckets[3] = { 0, 0, 0 };    
ifstream inputFile;

inputFile.open("input");

while (inputFile >> reading)
{
    // Fill the buckets
    buckets[0] += reading; buckets[1] += reading; buckets[2] += reading;

    if (buckets[i] > baseSum) result++;

    // Empty bucket, set up for next loop
    baseSum = buckets[i];
    buckets[i] = 0;
    i = ++i % 3;
}

cout << "Number of increases: " << result;

That was fun, if quick. The next days should be a bit more tricky, I hope.


My solutions for today's puzzles