Files
AdventOfCode/2022/1.js
benkyd 378a972285 day1
2022-12-01 11:12:04 +00:00

20 lines
505 B
JavaScript

const fs = require('fs');
let elfIndex = 0;
const calorieAmounts = fs.readFileSync('1.input').toString().split(`\n`).reduce((a, c) => {
if (c == '') {
elfIndex++;
a[elfIndex] = 0;
return a;
}
a[elfIndex] += parseInt(c);
return a;
}, [0]);
const part1 = Math.max(...calorieAmounts);
console.log(`PART 1: ${part1}`);
const sortedCalories = calorieAmounts.sort((a, b) => b - a);
console.log(`PART 2: ${sortedCalories[0] + sortedCalories[1] + sortedCalories[2]}`);