Public
Edited
Jan 11, 2023
Insert cell
Insert cell
Insert cell
Insert cell
function parseTopology(rawInput) {
return rawInput
.split("\n")
.map(row => row
.split("")
.map(c => c.charCodeAt(0)));
}
Insert cell
Insert cell
Insert cell
function findElevation(elevation, topology) {
for (const [r, row] of topology.entries()) {
for (const [c, e] of row.entries()) {
if (e === elevation) return [r, c];
}
}

return [-1, -1];
}
Insert cell
function printPath(topology, len = 3) {
const pathMap = topology
.map(row => row
.map(e => String(e))
.map(e => e.length < len ? e.padStart(len, "#") : e)
.join(" "))
.join("\n");
console.log(pathMap);
}
Insert cell
Insert cell
function findShortestPathDijkstra(topology) {
const start = findElevation(83, topology).join("");
const distances = new Map();
const previous = new Map();
const unvisited = new Set();
for (const [r, row] of topology.entries()) {
for (const [c] of row.entries()) {
distances.set(`${r}${c}`, Infinity);
previous.set(`${r}${c}`, undefined);
unvisited.add(`${r}${c}`);
}
}
distances.set(start, 0);

while (unvisited.size > 0) {
}
// console.log({distances, previous, size: unvisited.size});
// const distances = Array(topology.length).fill(Array(topology[0].length).fill(Infinity));
// const previous = Array(topology.length).fill(Array(topology[0].length).fill(undefined));
// const [startR, startC] = findElevation(83, topology);
// distances[startR][startC] = 0;
}
Insert cell
// findShortestPath(testTopology)
Insert cell
findShortestPathDijkstra(testTopology)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more