-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022_1.js
69 lines (67 loc) · 1.7 KB
/
2022_1.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const fs = require("fs");
let string = fs.readFileSync("2022_1_data.txt").toString();
let highest = 0;
let secondHighest = 0;
let thirdHighest = 0;
let splitString = string.split("\n");
let subTotal = 0;
splitString.forEach((number, idx) => {
if (number) subTotal += parseInt(number);
else {
if (subTotal > highest) {
highest = subTotal;
console.log("highest now: ", highest);
}
subTotal = 0;
}
if (idx == splitString.length - 1) {
if (subTotal > highest) {
highest = subTotal;
console.log("highest now: ", highest);
}
subTotal = 0;
}
});
splitString.forEach((number, idx) => {
if (number) subTotal += parseInt(number);
else {
if (subTotal > secondHighest && subTotal != highest) {
secondHighest = subTotal;
console.log("second highest now: ", secondHighest);
}
subTotal = 0;
}
if (idx == splitString.length - 1) {
if (subTotal > secondHighest && subTotal != highest) {
secondHighest = subTotal;
console.log("second highest now: ", secondHighest);
}
subTotal = 0;
}
});
splitString.forEach((number, idx) => {
if (number) subTotal += parseInt(number);
else {
if (
subTotal > thirdHighest &&
subTotal != highest &&
subTotal != secondHighest
) {
thirdHighest = subTotal;
console.log("third highest now: ", thirdHighest);
}
subTotal = 0;
}
if (idx == splitString.length - 1) {
if (
subTotal > thirdHighest &&
subTotal != highest &&
subTotal != secondHighest
) {
thirdHighest = subTotal;
console.log("third highest now: ", thirdHighest);
}
subTotal = 0;
}
});
console.log("total = ", highest + secondHighest + thirdHighest)