John Shaver 2 months ago
parent 02e744c3c7
commit 31e86a06dc

@ -1,28 +1,25 @@
const fs = require('fs/promises');
const main = async (filePath) => {
const input = await fs.readFile(filePath, 'utf8');
const elves = input.split('\n\n');
console.log("Elves: ", elves.slice(0, 5))
const result = elves.reduce(([highestCal, fatestElf], currentFood, currentElfIndex) => {
const currentCal = sumArray(currentFood.split('\n'));
console.log("Current cal: ", currentCal);
if(currentCal >= highestCal) {
return [currentCal, currentElfIndex];
}
return [highestCal, fatestElf];
}, [0, -1]);
console.log("Input: ", input.slice(0,100));
const elfTotals = input.split('\n\n').map(elfStr => sumArray(elfStr.split('\n')));
return result;
}
console.log("elfTotals: ", elfTotals.slice(0, 5));
elfTotals.sort((a,b) => b-a);
const sumArray = (values) =>
values.reduce((a,b) => parseFloat(a) + parseFloat(b), 0);
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!");

Loading…
Cancel
Save