summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-12-01 13:54:16 +0300
committereug-vs <eugene@eug-vs.xyz>2022-12-01 13:54:16 +0300
commit8d915727b98c41ca25de745a01f9d56df95d070d (patch)
tree5930fd10c043d80dfe4eaf39b16b9a23dfb6d886
parent9ee5ab609d980c41234c384f3e8ef39b1575de73 (diff)
downloadaoc-2022-8d915727b98c41ca25de745a01f9d56df95d070d.tar.gz
feat(day1): solve part 2
-rw-r--r--day-1/index.js11
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);