aboutsummaryrefslogtreecommitdiff
path: root/2020/09/solve.js
blob: f1289c76b0e187b96ae965e4f10a05d7138163a4 (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
46
47
48
49
const { _, performance } = require("perf_hooks");
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 tic = performance.now();
const num = partOne();
console.log(num);
console.log(partTwo(num));
const toc = performance.now();
console.log("TIME: " + ((toc - tic) / 1000).toFixed(6) + " seconds");