advent-of-code/puzzles/1/index.js

29 lines
638 B
JavaScript
Raw Normal View History

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