Skip to content

Instantly share code, notes, and snippets.

@edorivai
Created December 3, 2021 08:22
Show Gist options
  • Save edorivai/36620252e8dbb256a3b31ce3464cc8a9 to your computer and use it in GitHub Desktop.
Save edorivai/36620252e8dbb256a3b31ce3464cc8a9 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const file = fs.readFileSync(__dirname + "/input.txt", { encoding: "utf-8" });
const depths = file.split('\n').map(Number);
const threeMeasurements = depths.map((first, index) => {
if (index > depths.length - 3) return null;
const second = depths[index + 1];
const third = depths[index + 2];
return first + second + third;
}).filter(x => x != null);
const increaseCount = threeMeasurements.reduce((count, curr, index) => {
const prev = threeMeasurements[index - 1];
if (prev && prev < curr) return count + 1;
return count;
}, 0);
console.log('depths:', depths.length);
console.log('threes:', threeMeasurements.length);
console.log('increases:', increaseCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment