adventofcode2022/01/code.js
2022-12-01 00:21:53 -05:00

32 lines
665 B
JavaScript

const fs = require('fs');
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
// Part One
let caloriesArray = [];
let calories = 0;
for (i in inputArray) {
const value = parseInt(inputArray[i]);
if (value > 0) {
calories += value;
} else {
caloriesArray.push(calories);
calories = 0;
}
}
console.log(Math.max(...caloriesArray));
// Part Two
const first = Math.max(...caloriesArray);
caloriesArray.splice(caloriesArray.indexOf(first), 1);
const second = Math.max(...caloriesArray);
caloriesArray.splice(caloriesArray.indexOf(second), 1);
const third = Math.max(...caloriesArray);
console.log(first + second + third);