-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
88 lines (81 loc) · 1.95 KB
/
solution.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import fs from 'fs/promises';
const input = await fs.readFile('./input.txt', 'utf8');
const lines = input.trim().split('\n');
const ROPE_COUNT = process.env.part === 'part2' ? 9 : 1;
// [[x, y]]
const VISITED = [[0, 0]];
const H_POS = [0, 0];
const K_POS = [];
for (let n = 0; n < ROPE_COUNT; n++) {
K_POS.push([0, 0]);
}
for (const line of lines) {
const dir = line.charAt(0);
let n = parseInt(line.slice(2));
while (n--) {
switch (dir) {
case 'U':
H_POS[1]--;
break;
case 'R':
H_POS[0]++;
break;
case 'D':
H_POS[1]++;
break;
case 'L':
H_POS[0]--;
break;
default:
throw new Error(`Unknown dir ${dir}`);
}
for (let n = 0; n < ROPE_COUNT; n++) {
// Next knot position
const prev = n ? K_POS[n - 1] : H_POS;
const diff = `${K_POS[n][0] - prev[0]}${K_POS[n][1] - prev[1]}`;
switch (diff) {
case '0-2': // above
K_POS[n][1]++;
break;
case '1-2': // above/right
case '2-2':
case '2-1':
K_POS[n][1]++;
K_POS[n][0]--;
break;
case '-1-2': // above/left
case '-2-2':
case '-2-1':
K_POS[n][1]++;
K_POS[n][0]++;
break;
case '20': // right
K_POS[n][0]--;
break;
case '-20': // left
K_POS[n][0]++;
break;
case '02': // below
K_POS[n][1]--;
break;
case '12': // below/right
case '22':
case '21':
K_POS[n][1]--;
K_POS[n][0]--;
break;
case '-12': // below/left
case '-22':
case '-21':
K_POS[n][1]--;
K_POS[n][0]++;
break;
}
}
VISITED.push([...K_POS[K_POS.length - 1]]);
}
}
const UNIQUE = VISITED.filter((pos) =>
pos === VISITED.findLast(([x, y]) => pos[0] === x && pos[1] === y)
);
console.log(UNIQUE.length);