-
Notifications
You must be signed in to change notification settings - Fork 5
/
day14.mjs
132 lines (116 loc) · 3.25 KB
/
day14.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { readFileSync } from "node:fs";
const lines = readFileSync("day14.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
// Return a new object to avoid side effects between part 1 and 2
function getInput() {
const map = new Set();
let maxY = 0;
//498,4 -> 498,6 -> 496,6
for (const line of lines) {
const points = line.split(" -> ").map((point) => {
const [x, y] = point.split(",").map(Number);
if (y > maxY) {
maxY = y;
}
return { x, y };
});
let currentPoint = points.shift();
while (points.length) {
let targetPoint = points.shift();
// Draw a line between currentPoint and targetPoint
while (
currentPoint.x !== targetPoint.x ||
currentPoint.y !== targetPoint.y
) {
map.add(`${currentPoint.x},${currentPoint.y}`);
if (currentPoint.x !== targetPoint.x) {
const delta =
(targetPoint.x - currentPoint.x) /
Math.abs(targetPoint.x - currentPoint.x);
currentPoint.x += delta;
} else {
const delta =
(targetPoint.y - currentPoint.y) /
Math.abs(targetPoint.y - currentPoint.y);
currentPoint.y += delta;
}
}
map.add(`${currentPoint.x},${currentPoint.y}`);
}
}
return { map, maxY };
}
function part1() {
const { map, maxY } = getInput();
let sandIntoEndlessVoid = false;
let sandUnits = 0;
while (!sandIntoEndlessVoid) {
//spawn sand
let point = { x: 500, y: 0 }; // assume this point is always empty
sandUnits++;
// drop the sand
while (!sandIntoEndlessVoid) {
if (!map.has(`${point.x},${point.y + 1}`)) {
// direct fall
point.y++;
} else if (!map.has(`${point.x - 1},${point.y + 1}`)) {
// diagonal fall to the left
point.y++;
point.x--;
} else if (!map.has(`${point.x + 1},${point.y + 1}`)) {
// diagonal fall to the right
point.y++;
point.x++;
} else {
// can't fall further
map.add(`${point.x},${point.y}`);
break;
}
if (point.y >= maxY) {
sandIntoEndlessVoid = true;
sandUnits--;
}
}
}
console.log(sandUnits);
}
function part2() {
const { map, maxY } = getInput();
let sandUnits = 0;
while (true) {
if (map.has(`500,0`)) {
break;
}
//spawn sand
let point = { x: 500, y: 0 };
sandUnits++;
// drop the sand
while (true) {
if (point.y === maxY + 1) {
// reached the bottom floor
map.add(`${point.x},${point.y}`);
break;
} else if (!map.has(`${point.x},${point.y + 1}`)) {
// direct fall
point.y++;
} else if (!map.has(`${point.x - 1},${point.y + 1}`)) {
// diagonal fall to the left
point.y++;
point.x--;
} else if (!map.has(`${point.x + 1},${point.y + 1}`)) {
// diagonal fall to the right
point.y++;
point.x++;
} else {
// can't fall further
map.add(`${point.x},${point.y}`);
break;
}
}
}
console.log(sandUnits);
}
part1();
part2();