-
Notifications
You must be signed in to change notification settings - Fork 5
/
day09.mjs
106 lines (95 loc) · 2.31 KB
/
day09.mjs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { readFileSync } from "node:fs";
const lines = readFileSync("day09.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.trim() // Remove starting/ending whitespace
.split("\n") // Split on newline
.map((line) => {
const [letter, number] = line.split(" ");
return {
direction: letter,
totalMoves: parseInt(number),
};
});
const movesDefinition = {
R: {
x: 1,
y: 0,
},
L: {
x: -1,
y: 0,
},
U: {
x: 0,
y: -1,
},
D: {
x: 0,
y: 1,
},
};
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
move(direction) {
const delta = movesDefinition[direction];
this.x += delta.x;
this.y += delta.y;
}
follow(point) {
const distance = Math.max(
Math.abs(this.x - point.x),
Math.abs(this.y - point.y)
);
if (distance > 1) {
// Move this point
const directionX = point.x - this.x;
// 0 => do nothing
// 1 or 2 => this.x++;
// -1 or -2 => this.x--;
this.x += Math.abs(directionX) === 2 ? directionX / 2 : directionX;
const directionY = point.y - this.y;
this.y += Math.abs(directionY) === 2 ? directionY / 2 : directionY;
}
}
}
function markVisited(x, y, visited) {
visited.add(`${x}-${y}`);
}
function part1() {
const head = new Point(0, 0);
const tail = new Point(0, 0);
const visited = new Set();
markVisited(0, 0, visited);
for (const line of lines) {
for (let i = 0; i < line.totalMoves; i++) {
head.move(line.direction);
tail.follow(head);
markVisited(tail.x, tail.y, visited);
}
}
console.log(visited.size);
}
function part2() {
const knots = new Array(10).fill(0).map((_) => new Point(0, 0));
const visited = new Set();
markVisited(0, 0, visited);
for (const line of lines) {
for (let i = 0; i < line.totalMoves; i++) {
// Move the head according to the instructions
knots[0].move(line.direction);
// Move the rest of the rope
for (let knot = 1; knot < knots.length; knot++) {
const point = knots[knot];
point.follow(knots[knot - 1]);
}
const tail = knots[knots.length - 1];
markVisited(tail.x, tail.y, visited);
}
}
console.log(visited.size);
}
part1();
part2();