mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-27 04:21:45 +00:00
calculate and sum all distances
This commit is contained in:
parent
d1c56a4317
commit
de288ab19c
@ -25,7 +25,14 @@ const galaxies = universe
|
|||||||
.map((a, y) => a.map((b, x) => ({ s: b, x, y })).filter((b) => b.s === "#"))
|
.map((a, y) => a.map((b, x) => ({ s: b, x, y })).filter((b) => b.s === "#"))
|
||||||
.filter((e) => e.length > 0)
|
.filter((e) => e.length > 0)
|
||||||
.reduce((a, i) => [...a, ...i], []);
|
.reduce((a, i) => [...a, ...i], []);
|
||||||
console.log(galaxies);
|
const distances = galaxies.map((c, i) =>
|
||||||
|
galaxies.reduce(
|
||||||
|
(a, n, j) => a + (j > i ? Math.abs(c.x - n.x + c.y - n.y) : 0),
|
||||||
|
0
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const sum = distances.reduce((a, i) => a + i);
|
||||||
|
console.log(sum);
|
||||||
|
|
||||||
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