From 403830daee9e1a353bdec2488549ce8e04cf02cc Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Fri, 15 Dec 2023 19:52:59 +0100 Subject: [PATCH] store positions, expand by incrementing --- Day11/Part2.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Day11/Part2.js b/Day11/Part2.js index 20076ae..4fc22d2 100644 --- a/Day11/Part2.js +++ b/Day11/Part2.js @@ -5,24 +5,30 @@ const input = readFileSync("input.txt") .toString() .split(/\r*\n/) .filter((e) => e.length > 0) - .map((e) => e.split("")); + .map((a, j) => a.split("").map((b, i) => ({ s: b, x: i, y: j }))); -const expand = (arr) => - arr.reduce( - (a, i) => [ - ...a, - ...(i.some((e) => e !== ".") ? [i] : [i, Array(i.length).fill(".")]), - ], - [] - ); +const expand = (arr) => { + let expansion = 0; + return arr.map((a) => { + const c = !a.some((b) => b.s !== "."); + if (c) expansion += 999999; + return a.map((b) => { + const { s, x, y } = b; + return { s, x, y: y + expansion }; + }); + }); +}; const flip = (current) => - Array.from({ length: current[0].length }, (_, x) => - Array.from({ length: current.length }, (_, y) => current[y][x]) + Array.from({ length: current[0].length }, (_, cx) => + Array.from({ length: current.length }, (_, cy) => { + const { s, x, y } = current[cy][cx]; + return { s, x: y, y: x }; + }) ); const universe = flip(expand(flip(expand(input)))); const galaxies = universe - .map((a, y) => a.map((b, x) => ({ s: b, x, y })).filter((b) => b.s === "#")) + .map((a) => a.filter((b) => b.s === "#")) .filter((e) => e.length > 0) .reduce((a, i) => [...a, ...i], []); const distances = galaxies.map((c, i) =>