Public
Edited
Dec 16, 2023
Insert cell
Insert cell
Insert cell
Insert cell
lines = input.split("\n").map((x) => Array.from(x))
Insert cell
Insert cell
roll = (line) => {
const segments = line
.join("")
.split("#")
.map((x) => Array.from(x))
.map((item) => {
const counts = _.countBy(item);
return [
...Array.from({ length: counts["O"] ?? 0 }).fill("O"),
...Array.from({ length: counts["."] ?? 0 }).fill(".")
].join("");
})
.join("#");
return Array.from(segments);
}
Insert cell
Insert cell
rollNorth = (lines) => _.zip(..._.zip(...lines).map((line) => roll(line)))
Insert cell
score = (lines) =>
_.sum(
lines.map(
(line, i) => line.filter((x) => x === "O").length * (lines.length - i)
)
)
Insert cell
part1 = score(rollNorth(lines))
Insert cell
Insert cell
Insert cell
rollSouth = (lines) => _.reverse(rollNorth(_.reverse([...lines])))
Insert cell
rollWest = (lines) => lines.map((line) => roll(line))
Insert cell
rollEast = (lines) =>
rollWest(lines.map((line) => [...line].reverse())).map((line) =>
line.reverse()
)
Insert cell
cycle = (lines) => rollEast(rollSouth(rollWest(rollNorth(lines))))
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
newLine = (lines) => {
let curr = lines;
for (let i = 0; i < 81 + 28; i++) {
curr = cycle(curr);
}
return curr;
}
Insert cell
findCycle = (line) => {
let p1 = cycle(line);
let p2 = cycle(cycle(line));

while (!_.isEqual(p1, p2)) {
p1 = cycle(p1);
p2 = cycle(cycle(p2));
}

let mu = 0;
p1 = line;
while (!_.isEqual(p1, p2)) {
p1 = cycle(p1);
p2 = cycle(p2);
mu++;
}
let lambda = 1;
p2 = cycle(p1);
while (!_.isEqual(p1, p2)) {
p2 = cycle(p2);
lambda++;
}
return [mu, lambda];
}
Insert cell
(1000000000 - 81) % 51
Insert cell
findCycle(lines)
Insert cell
part2 = score(newLine(lines))
Insert cell
Insert cell
Insert cell
input = textarea || select.value
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more