blob: 394bd4bd14a97f1c611bb286726a07a966095c71 (
plain) (
blame)
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
|
const fs = require("fs");
const data = fs
.readFileSync("input", "utf8")
.split("\n")
.map((x) => parseInt(x));
function partOne() {
for (var i = 25; i < data.length; i++) {
const prev = data.slice(i - 25, i);
let correct = false;
for (let x = 0; x < prev.length; x++) {
for (let y = 0; y < prev.length; y++) {
if (prev[x] + prev[y] === data[i]) {
correct = true;
break;
}
}
if (correct) break;
}
if (!correct) return data[i];
}
return 0;
}
function partTwo(num) {
for (let i = 0; i < data.length; i++) {
let cnt = 0;
let j = i;
for (; j < data.length; j++) {
cnt += data[j];
if (cnt >= num) break;
}
if (cnt == num) {
const range = data.slice(i, j + 1);
return Math.min(...range) + Math.max(...range);
} else {
cnt = 0;
}
}
}
const num = partOne();
console.log(num);
console.log(partTwo(num));
|