Public
Edited
Dec 6, 2022
2 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
known00 = move0(input00)
Insert cell
puzzle00 = move0(input01)
Insert cell
move0 = function (input) {
const [stacksRaw, movesRaw] = input.split("\n\n");

// Get moves b/c they're easy to get
const dslRegexp = /move ([?:\d]+) from ([?:\d]+) to ([?:\d]+)/g;

// this is now an array of arrays;
// each array is [# to move, from which stack, to which stack]
const moves = [...movesRaw.matchAll(dslRegexp)].map((d) =>
d.slice(1).map((n) => parseInt(n))
);

// need each row as a separate array
const stacksSplit = stacksRaw.split("\n");

// get the number of stacks from the last line
const numberOfStacks = parseInt(
stacksSplit
.slice(-1)[0]
.split(/\s+/g)
.filter((d) => d != "")
.slice(-1)[0]
);

// build a dynamic regexp string since we won't know the number of columns
const stacksRegexp = new RegExp(
"(.{3}) ".repeat(numberOfStacks).replace(/.$/, ""),
"gm"
);

// use the regexp to find all the matches;
// remove the bottom row which are the #'s we got way above
var stacks = [...stacksRaw.matchAll(stacksRegexp)]
.map((d) => d.slice(1))
.slice(0, -1);

// rows to columns, removing blanks and getting rid of brackets
stacks = stacks[0]
.map((col, idx) => stacks.map((row) => row[idx]))
.map((col) =>
col.filter((d) => d != " ").map((d) => d.replace(/[\[\]]/g, ""))
);

// move the boxes around!
moves.forEach(([nBoxes, fromStack, toStack]) => {
const moved = stacks[fromStack - 1].splice(0, nBoxes);
stacks[toStack - 1].unshift(...moved.reverse());
});

// get the first element from each stack and smush them together
return stacks.map((d) => d[0]).join("");
}
Insert cell
Insert cell
Insert cell
known01 = move1(input00)
Insert cell
puzzle01 = move1(input01)
Insert cell
move1 = function (input) {
const [stacksRaw, movesRaw] = input.split("\n\n");

const dslRegexp = /move ([?:\d]+) from ([?:\d]+) to ([?:\d]+)/g;

const moves = [...movesRaw.matchAll(dslRegexp)].map((d) =>
d.slice(1).map((n) => parseInt(n))
);

const stacksSplit = stacksRaw.split("\n");

const numberOfStacks = parseInt(
stacksSplit
.slice(-1)[0]
.split(/\s+/g)
.filter((d) => d != "")
.slice(-1)[0]
);

const stacksRegexp = new RegExp(
"(.{3}) ".repeat(numberOfStacks).replace(/.$/, ""),
"gm"
);

var stacks = [...stacksRaw.matchAll(stacksRegexp)]
.map((d) => d.slice(1))
.slice(0, -1);

stacks = stacks[0]
.map((col, idx) => stacks.map((row) => row[idx]))
.map((col) =>
col.filter((d) => d != " ").map((d) => d.replace(/[\[\]]/g, ""))
);

moves.forEach(([nBoxes, fromStack, toStack]) => {
const moved = stacks[fromStack - 1].splice(0, nBoxes);
stacks[toStack - 1].unshift(...moved); // THE ONLY LINE THAT CHANGED
});

return stacks.map((d) => d[0]).join("");
}
Insert cell
Insert cell
input00 = FileAttachment("input00.txt").text()
Insert cell
input01 = FileAttachment("input01.txt").text()
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