-
Notifications
You must be signed in to change notification settings - Fork 5
/
day12.mjs
176 lines (162 loc) · 4 KB
/
day12.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { readFileSync } from "node:fs";
const lines = readFileSync("day12.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 res = {
start: {},
end: {},
map: [],
};
res.map = lines.map((line, y) =>
[...line].map((value, x) => {
if (value === "S") {
res.start = {
y,
x,
};
return 0;
}
if (value === "E") {
res.end = { y, x };
return 25;
}
return value.charCodeAt(0) - "a".charCodeAt(0);
})
);
return res;
}
function pointToInt(x, y) {
return y * 1e3 + x;
}
function intToPoint(int) {
return {
y: Math.floor(int / 1e3),
x: int % 1e3,
};
}
function getNeighbors(x, y, map) {
const res = [];
if (y + 1 < map.length && map[y + 1][x] <= map[y][x] + 1) {
res.push(pointToInt(x, y + 1));
}
if (y - 1 >= 0 && map[y - 1][x] <= map[y][x] + 1) {
res.push(pointToInt(x, y - 1));
}
if (x + 1 < map[y].length && map[y][x + 1] <= map[y][x] + 1) {
res.push(pointToInt(x + 1, y));
}
if (x - 1 >= 0 && map[y][x - 1] <= map[y][x] + 1) {
res.push(pointToInt(x - 1, y));
}
return res;
}
function dijkstra(map, start, end) {
const dist = {};
const prev = {};
let queue = [];
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
const id = pointToInt(x, y);
dist[id] = Infinity;
// prev[pointToInt(x, y)] = ;
queue.push(id);
}
}
dist[pointToInt(start.x, start.y)] = 0;
while (queue.length) {
let u = null;
for (const current of queue) {
if (u === null || dist[current] < dist[u]) {
u = current;
}
}
if (u === pointToInt(end.x, end.y)) {
break;
}
queue = queue.filter((x) => x !== u);
const point = intToPoint(u);
const neighbors = getNeighbors(point.x, point.y, map);
for (const v of neighbors) {
if (queue.includes(v)) {
const alt = dist[u] + 1;
if (alt < dist[v]) {
dist[v] = alt;
prev[v] = u;
}
}
}
}
return {
dist,
prev,
};
}
function part1() {
const input = getInput();
const data = dijkstra(input.map, input.start, input.end);
const distance = data.dist[pointToInt(input.end.x, input.end.y)];
console.log(distance);
}
function getNeighbors2(x, y, map) {
const res = [];
if (y + 1 < map.length && map[y + 1][x] >= map[y][x] - 1) {
res.push(pointToInt(x, y + 1));
}
if (y - 1 >= 0 && map[y - 1][x] >= map[y][x] - 1) {
res.push(pointToInt(x, y - 1));
}
if (x + 1 < map[y].length && map[y][x + 1] >= map[y][x] - 1) {
res.push(pointToInt(x + 1, y));
}
if (x - 1 >= 0 && map[y][x - 1] >= map[y][x] - 1) {
res.push(pointToInt(x - 1, y));
}
return res;
}
function dijkstra2(map, start, end) {
const dist = {};
const prev = {};
let queue = [];
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
const id = pointToInt(x, y);
dist[id] = Infinity;
// prev[pointToInt(x, y)] = ;
queue.push(id);
}
}
dist[pointToInt(start.x, start.y)] = 0;
while (queue.length) {
let u = null;
for (const current of queue) {
if (u === null || dist[current] < dist[u]) {
u = current;
}
}
const point = intToPoint(u);
if (map[point.y][point.x] === 0) {
return dist[u];
}
queue = queue.filter((x) => x !== u);
const neighbors = getNeighbors2(point.x, point.y, map);
for (const v of neighbors) {
if (queue.includes(v)) {
const alt = dist[u] + 1;
if (alt < dist[v]) {
dist[v] = alt;
prev[v] = u;
}
}
}
}
}
function part2() {
const input = getInput();
const distance = dijkstra2(input.map, input.end);
console.log(distance);
}
part1();
part2();