adventofcode2021/01/code.js
2021-12-01 15:52:20 -05:00

40 lines
587 B
JavaScript

const fs = require('fs');
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
// Part One
let increases = 0;
let previous;
for(i in inputArray) {
const value = parseInt(inputArray[i]);
if (previous && value > previous) {
increases++;
}
previous = value;
}
console.log(increases);
// Part Two
increases = 0;
previous = null;
let a;
let b;
let c;
for(i in inputArray) {
c = parseInt(inputArray[i]);
total = a + b + c;
if (a && b && c && total > previous) {
increases++;
}
a = b;
b = c;
previous = total;
}
console.log(increases);