summaryrefslogtreecommitdiff
path: root/day-1/index.js
blob: 31490b6591f68c8954ff5b4d4d871d307a583ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 === '') {
    acc.sums.push(acc.current);
    acc.current = 0;
  } else acc.current += parseInt(line);

  return acc;
}, { current: 0, sums: [] });


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);