-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone.js
More file actions
37 lines (28 loc) · 659 Bytes
/
Copy pathone.js
File metadata and controls
37 lines (28 loc) · 659 Bytes
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
const { readFile } = require('fs/promises');
// 1a-b
async function init() {
const input = await readFile('one.txt', 'utf8');
// const input = await readFile('one.test.txt', 'utf8');
const data = input.split('\n').map((d) => parseInt(d));
const elfs = [];
let i = 0;
let max = 0;
data.forEach((d) => {
if (!elfs[i]) {
elfs[i] = 0;
}
if (isNaN(d)) {
if (elfs[i] > max) {
max = elfs[i];
}
i++;
return;
}
elfs[i] += d;
});
console.log(elfs);
console.log(max);
const sorted = elfs.sort((a, b) => a - b).reverse();
console.log(sorted[0] + sorted[1] + sorted[2]);
}
init();