-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.ts
28 lines (24 loc) · 864 Bytes
/
part2.ts
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
import * as fs from 'fs';
const input = fs
.readFileSync('input', 'utf8')
.split('\n')
.map((x) => x.split(' ').map((y: string | number, i) => (y = i === 1 ? +y : y)));
const knots = new Array(10).fill(null).map(() => ({ x: 0, y: 0 }));
const visited = new Set([`0,0`]);
for (const [direction, count] of input) {
for (let i = 0; i < (count as number); i++) {
if (direction === 'R') knots[0].x++;
if (direction === 'L') knots[0].x--;
if (direction === 'D') knots[0].y++;
if (direction === 'U') knots[0].y--;
for (let j = 1; j < knots.length; j++) {
const [head, tail] = [knots[j - 1], knots[j]];
if (Math.abs(head.x - tail.x) === 2 || Math.abs(head.y - tail.y) === 2) {
tail.x += Math.sign(head.x - tail.x);
tail.y += Math.sign(head.y - tail.y);
}
}
visited.add(`${knots[9].x},${knots[9].y}`);
}
}
console.log(visited.size);