This commit is contained in:
John Shaver 2022-12-01 10:27:17 -08:00
parent 02e744c3c7
commit 31e86a06dc

View file

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