From 31e86a06dce6e7284021246e005841f676def4bd Mon Sep 17 00:00:00 2001 From: John Shaver Date: Thu, 1 Dec 2022 10:27:17 -0800 Subject: [PATCH] 1.2 --- puzzles/1/index.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/puzzles/1/index.js b/puzzles/1/index.js index 9d2011c..b249387 100644 --- a/puzzles/1/index.js +++ b/puzzles/1/index.js @@ -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); + + console.log("Sorted elfTotals: ", elfTotals.slice(0, 5)); + + + return sumArray(elfTotals.slice(0,3)); } -const sumArray = (values) => - values.reduce((a,b) => parseFloat(a) + parseFloat(b), 0); - - +const sumArray = array => array.reduce((a,b) => parseFloat(a) + parseFloat(b), 0); main("./input.txt").then(result => { console.log("Done!");