32 lines
753 B
JavaScript
32 lines
753 B
JavaScript
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]);
|
|
|
|
return result;
|
|
}
|
|
|
|
const sumArray = (values) =>
|
|
values.reduce((a,b) => parseFloat(a) + parseFloat(b), 0);
|
|
|
|
|
|
|
|
main("./input.txt").then(result => {
|
|
console.log("Done!");
|
|
|
|
console.log("Result: ", result);
|
|
});
|