Public
Edited
Dec 14, 2023
Insert cell
Insert cell
show(slideNorth([...input]), w)
Insert cell
function slideNorth(input) {
for (let x = 0; x < w; ++x) {
let block = -1;
for (let i = 0; i < w; ++i) {
switch (input[i * (w + 1) + x]) {
case "O":
input[i * (w + 1) + x] = " ";
input[++block * (w + 1) + x] = "O";
break;
case "#":
block = i;
break;
case ".":
break;
}
}
}
return input;
}
Insert cell
load = (input, w) =>
d3.sum(input, (d, i) => (d === "O") * (w - Math.floor(i / (w + 1))))
Insert cell
answer1 = load(slideNorth([...input]), w)
Insert cell
Insert cell
answer2 = load(rocks, 100)
Insert cell
Insert cell
viewof rocks = {
replay;

const A = input;
const M = [...A];
const w = A.indexOf("\n");

function cycle() {
slideNorth();
slideWest();
slideSouth();
slideEast();
}

function slideEast() {
for (let y = 0; y < w; ++y) {
let block = w;
for (let i = w - 1; i >= 0; --i) {
const pos = i + y * (w + 1);
switch (M[pos]) {
case "O":
M[pos] = " ";
M[--block + y * (w + 1)] = "O";
break;
case "#":
block = i;
break;
case ".":
break;
}
}
}
}

function slideSouth() {
for (let x = 0; x < w; ++x) {
let block = w;
for (let i = w - 1; i >= 0; --i) {
switch (M[i * (w + 1) + x]) {
case "O":
M[i * (w + 1) + x] = " ";
M[--block * (w + 1) + x] = "O";
break;
case "#":
block = i;
break;
case ".":
break;
}
}
}
}

function slideWest() {
for (let y = 0; y < w; ++y) {
let block = -1;
for (let i = 0; i < w; ++i) {
const pos = i + y * (w + 1);
switch (M[pos]) {
case "O":
M[pos] = " ";
M[++block + y * (w + 1)] = "O";
break;
case "#":
block = i;
break;
case ".":
break;
}
}
}
}

function slideNorth() {
for (let x = 0; x < w; ++x) {
let block = -1;
for (let i = 0; i < w; ++i) {
switch (M[i * (w + 1) + x]) {
case "O":
M[i * (w + 1) + x] = " ";
M[++block * (w + 1) + x] = "O";
break;
case "#":
block = i;
break;
case ".":
break;
}
}
}
}

const seen = new Map(); // cycle detection
let i, j;
const N = 1000000000;
for (i = 0; i < N; ++i) {
cycle();
const key = M.join("");
if (seen.has(key)) {
j = seen.get(key);
break; // cycled!
}
seen.set(key, i);
yield show(M, w);
}

// we detected a cycle that started at j and repeats at i
// now we need to reach N:
for (let k = (N - i) % (i - j); k > 0; --k) {
cycle();
yield show(M, w);
}

return yield show(M, w);
}
Insert cell
sample = `O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....`
Insert cell
Insert cell
w = input.indexOf("\n")
Insert cell
sample.indexOf("\n")
Insert cell
show = (rocks, w) =>
Object.assign(
html`<pre style="font-weight: bold;font-size:7px;letter-spacing:2.1px;line-height: 6.5px;"
>${rocks.join?.("")}`,
{ value: rocks }
)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more