-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.js
43 lines (32 loc) · 1.09 KB
/
day1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const fs = require('fs');
const day1a = () => {
// const filename = './data/day1test.txt';
const filename = './data/day1.txt';
const data = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' });
const lines = data.split('\n\n');
let maxCount = 0;
lines.forEach(line => {
const items = line.split('\n').map(i => +i);
const foodCount = items.reduce((prev, curr) => prev + curr);
maxCount = Math.max(maxCount, foodCount);
});
console.log('1a answer = ', maxCount);
}
const day1b = () => {
// const filename = './data/day1test.txt';
const filename = './data/day1.txt';
const data = fs.readFileSync(filename,
{ encoding: 'utf8', flag: 'r' });
const lines = data.split('\n\n');
let foodArr = [];
lines.forEach(line => {
const items = line.split('\n').map(i => +i);
const foodCount = items.reduce((prev, curr) => prev + curr);
foodArr.push(foodCount);
});
const sortedArr = foodArr.sort((a, b) => b - a);
// console.log(sortedArr);
console.log('1b answer = ', sortedArr[0] + sortedArr[1] + sortedArr[2]);
}
module.exports = { day1a, day1b }