From 8d915727b98c41ca25de745a01f9d56df95d070d Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 1 Dec 2022 13:54:16 +0300 Subject: feat(day1): solve part 2 --- day-1/index.js | 11 ++++++++--- 1 file 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); -- cgit v1.2.3