Skip to content

Instantly share code, notes, and snippets.

@p-a
Created December 26, 2021 20:59
Show Gist options
  • Save p-a/e67bc578b88196026fba4cb4e9ea22af to your computer and use it in GitHub Desktop.
Save p-a/e67bc578b88196026fba4cb4e9ea22af to your computer and use it in GitHub Desktop.
AoC 2021 Day 18 revisited
const flattenSnail = (snail, path = "") =>
Number.isInteger(snail)
? [[path, snail]]
: snail.flatMap((s, i) => flattenSnail(s, path + i));
const toSnail = (paths, path = "", map = new Map(paths)) =>
map.get(path) ?? [0, 1].map(v => toSnail(paths, path + v, map));
const explode = snail => {
const index = snail.findIndex(([path]) => path.length === 5);
if (index !== -1) {
if (index > 0) {
const l = snail[index][1];
snail[index - 1][1] += l;
}
if (index < snail.length - 2) {
const r = snail[index + 1][1];
snail[index + 2][1] += r;
}
return [
true,
[
...snail.slice(0, index),
[snail[index][0].slice(0, -1), 0],
...snail.slice(index + 2),
],
];
}
return [false, snail];
};
const split = snail => {
const index = snail.findIndex(([, value]) => value > 9);
if (index !== -1) {
const [path, value] = snail[index];
const [l, r] = [Math.floor, Math.ceil].map((f, i) => [
path + i,
f(value / 2),
]);
return [true, [...snail.slice(0, index), l, r, ...snail.slice(index + 1)]];
}
return [false, snail];
};
export const reduce = snail => {
let flatsnail = flattenSnail(snail);
let didChange = true;
while (didChange) {
[didChange, flatsnail] = [explode, split].reduce(
([changed, flatsnail], op) =>
changed ? [changed, flatsnail] : op(flatsnail),
[false, flatsnail]
);
}
return toSnail(flatsnail);
};
const magnitude = snail =>
snail
.map((v, i) => (i === 0 ? 3 : 2) * (Array.isArray(v) ? magnitude(v) : v))
.reduce((sum, v) => sum + v, 0);
export const part1 = snails =>
magnitude(
snails.map(JSON.parse).reduce((result, snail) => reduce([result, snail]))
);
export const part2 = snails =>
snails
.map(JSON.parse)
.flatMap((snail, i, snails) =>
[...Array(snails.length - i)].map((_, j) => [snail, snails[j + i]])
)
.flatMap(([a, b]) => [
[a, b],
[b, a],
])
.map(reduce)
.map(magnitude)
.reduce((max, v) => Math.max(max, v));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment