aboutsummaryrefslogtreecommitdiff
path: root/2020/23/solve.js
blob: f16443bf156a7af7066357f1b1f411a39e8bd2fc (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
const { _, performance } = require("perf_hooks");
const fs = require("fs");
const data = fs
  .readFileSync("input", "utf8")
  .split("\n")[0]
  .split("")
  .map((x) => parseInt(x));

function partOne() {
  let moves = 11;

  let cur = 0;
  const cups = data;
  while (--moves) {
    // Pick up
    const picks = [];
    for (let i = 0; i < 3; i++) picks[i] = cups.splice(cur + 1, 1);

    // Find destination
    let dest = 0;
    let decr = 1;
    while (!dest) {
      dest = cups.filter((x) => x == cups[cur] - decr)[0];
      if (!dest) decr++;

      if (decr > cups.length) {
        dest = Math.max.apply(Math, cups);
        break;
      }
    }

    cups.splice.apply(cups, [cups.indexOf(dest) + 1, 0].concat(picks));
    console.log("Cups: " + cups, "Picks: " + picks, "Dest: " + dest);

    cur++;
  }
  return 0;
}

function partTwo() {
  return 0;
}

const tic = performance.now();
console.log(partOne());
console.log(partTwo());
const toc = performance.now();
console.log("TIME: " + ((toc - tic) / 1000).toFixed(6) + " seconds");