advent-of-code/2022/1/index.js
2022-12-02 13:29:34 -08:00

28 lines
638 B
JavaScript

const fs = require('fs/promises');
const main = async (filePath) => {
const input = await fs.readFile(filePath, 'utf8');
console.log("Input: ", input.slice(0,100));
const elfTotals = input.split('\n\n').map(elfStr => sumArray(elfStr.split('\n')));
console.log("elfTotals: ", elfTotals.slice(0, 5));
elfTotals.sort((a,b) => b-a);
console.log("Sorted elfTotals: ", elfTotals.slice(0, 5));
return sumArray(elfTotals.slice(0,3));
}
const sumArray = array => array.reduce((a,b) => parseFloat(a) + parseFloat(b), 0);
main("./input.txt").then(result => {
console.log("Done!");
console.log("Result: ", result);
});