Skip to content

Instantly share code, notes, and snippets.

@reidblomquist
Last active December 3, 2021 02:54
Show Gist options
  • Save reidblomquist/678a08c2a7afc2ec8aef45cbd1a72c20 to your computer and use it in GitHub Desktop.
Save reidblomquist/678a08c2a7afc2ec8aef45cbd1a72c20 to your computer and use it in GitHub Desktop.
real shloppy advent of code day 1
const data = require('./data.json');
const sumArray = (array) => array.reduce((a, b) => a + b, 0);
const results = data.data.reduce((acc, n, idx, arr) => {
const lenIn = arr.length;
const isPastTupleTime = lenIn - idx <= 2;
const newAcc = {
...acc,
lastValue: n,
};
if (idx === 0) {
const newTuple = arr.slice(idx, idx + 3);
newAcc.lastWindow = newTuple;
newAcc.lastWindowSum = sumArray(newTuple);
return newAcc;
};
if (n > acc.lastValue) {
newAcc.rateIncreases += 1;
}
if (!isPastTupleTime) {
const newTuple = arr.slice(idx, idx + 3);
newAcc.lastWindow = newTuple;
newAcc.lastWindowSum = sumArray(newTuple);
if (newAcc.lastWindowSum > acc.lastWindowSum) {
newAcc.windowIncreases += 1;
}
}
return newAcc;
}, {
lastValue: 0,
rateIncreases: 0,
windowIncreases: 0,
lastWindow: [0, 0, 0],
lastWindowSum: 0,
});
console.log(results.rateIncreases);
console.log(results.windowIncreases);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment