store positions, expand by incrementing

This commit is contained in:
Baipyrus 2023-12-15 19:52:59 +01:00
parent 3f0d2874f8
commit 403830daee

View File

@ -5,24 +5,30 @@ const input = readFileSync("input.txt")
.toString() .toString()
.split(/\r*\n/) .split(/\r*\n/)
.filter((e) => e.length > 0) .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) => const expand = (arr) => {
arr.reduce( let expansion = 0;
(a, i) => [ return arr.map((a) => {
...a, const c = !a.some((b) => b.s !== ".");
...(i.some((e) => e !== ".") ? [i] : [i, Array(i.length).fill(".")]), if (c) expansion += 999999;
], return a.map((b) => {
[] const { s, x, y } = b;
); return { s, x, y: y + expansion };
});
});
};
const flip = (current) => const flip = (current) =>
Array.from({ length: current[0].length }, (_, x) => Array.from({ length: current[0].length }, (_, cx) =>
Array.from({ length: current.length }, (_, y) => current[y][x]) 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 universe = flip(expand(flip(expand(input))));
const galaxies = universe 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) .filter((e) => e.length > 0)
.reduce((a, i) => [...a, ...i], []); .reduce((a, i) => [...a, ...i], []);
const distances = galaxies.map((c, i) => const distances = galaxies.map((c, i) =>