mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-26 12:01:45 +00:00
calculate shortest path
This commit is contained in:
parent
14c6eec96f
commit
2e6ace81b7
@ -13,6 +13,7 @@ const input = readFileSync("input.txt")
|
|||||||
y: j,
|
y: j,
|
||||||
g: 0,
|
g: 0,
|
||||||
h: 0,
|
h: 0,
|
||||||
|
p: null,
|
||||||
})));
|
})));
|
||||||
const man_dist = (a, b) => Math.abs(b.x - a.x) + Math.abs(b.y - a.y);
|
const man_dist = (a, b) => Math.abs(b.x - a.x) + Math.abs(b.y - a.y);
|
||||||
|
|
||||||
@ -47,17 +48,36 @@ while (open.length > 0) {
|
|||||||
|
|
||||||
const nd = current.g + next.c;
|
const nd = current.g + next.c;
|
||||||
if (open.includes(next)) {
|
if (open.includes(next)) {
|
||||||
if (nd < next.g)
|
if (nd < next.g) {
|
||||||
next.g = nd;
|
next.g = nd;
|
||||||
|
next.p = current;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
next.g = nd;
|
next.g = nd;
|
||||||
|
next.p = current;
|
||||||
next.h = man_dist(next, end);
|
next.h = man_dist(next, end);
|
||||||
open.push(next);
|
open.push(next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(end.g);
|
console.log(end.g);
|
||||||
|
|
||||||
|
const shortest = [];
|
||||||
|
let path = end;
|
||||||
|
while (path) {
|
||||||
|
shortest.push(path);
|
||||||
|
path = path.p;
|
||||||
|
}
|
||||||
|
console.log(
|
||||||
|
input
|
||||||
|
.map(a => a
|
||||||
|
.map(b => shortest
|
||||||
|
.includes(b) ?
|
||||||
|
"*" : b.c
|
||||||
|
).join("")
|
||||||
|
).join("\n")
|
||||||
|
);
|
||||||
|
|
||||||
const t1 = performance.now();
|
const t1 = performance.now();
|
||||||
console.log(`Runtime: ${t1 - t0}ms`);
|
console.log(`Runtime: ${t1 - t0}ms`);
|
||||||
|
Loading…
Reference in New Issue
Block a user