From de288ab19cf2c82b0b827f452ef556f5646c23bf Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 12 Dec 2023 11:05:45 +0100 Subject: [PATCH] calculate and sum all distances --- Day11/Part1.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Day11/Part1.js b/Day11/Part1.js index 01905fc..194cab1 100644 --- a/Day11/Part1.js +++ b/Day11/Part1.js @@ -25,7 +25,14 @@ const galaxies = universe .map((a, y) => a.map((b, x) => ({ s: b, x, y })).filter((b) => b.s === "#")) .filter((e) => e.length > 0) .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(); console.log(`Runtime: ${t1 - t0}ms`);