diff options
Diffstat (limited to 'day-1')
-rw-r--r-- | day-1/index.js | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/day-1/index.js b/day-1/index.js index 512deea..31490b6 100644 --- a/day-1/index.js +++ b/day-1/index.js @@ -1,15 +1,20 @@ const fs = require('fs'); const input = fs.readFileSync('./day-1/input.txt').toString(); +const TOP_AMOUNT = 3; + const lines = input.split('\n'); const result = lines.reduce((acc, line) => { if (line === '') { - if (acc.current > acc.greatest) acc.greatest = acc.current; + acc.sums.push(acc.current); acc.current = 0; } else acc.current += parseInt(line); return acc; -}, { current: 0, greatest: 0 }); +}, { current: 0, sums: [] }); + -console.log(result); +const topSums = result.sums.sort((a, b) => b - a).slice(0, TOP_AMOUNT); +const totalTopSum = topSums.reduce((acc, value) => acc + value, 0); +console.log(totalTopSum); |