Public
Edited
May 7, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function createScreen(width, height) {
return Array.from({ length: height }, () => Array(width).fill("░"));
}
Insert cell
Insert cell
function rect(screen, [a, b]) {
for (let y = 0; y < b; y++) {
for (let x = 0; x < a; x++) {
screen[y][x] = "█";
}
}
return screen;
}
Insert cell
function rotateRow(screen, [row, n]) {
screen[row] = AOC.rotateArray(screen[row], n);
return screen;
}
Insert cell
function rotateColumn(screen, [col, n]) {
let column = screen.map((row) => row[col]);
column = AOC.rotateArray(column, n);
for (let row = 0; row < screen.length; row++) {
screen[row][col] = column[row];
}
return screen;
}
Insert cell
Insert cell
function execute(screen, tokens) {
if (tokens[0] === "rect") {
return rect(screen, tokens[1].split("x").map(Number));
}
const rotateParams = [Number(tokens[2].split("=")[1]), Number(tokens[4])];
return tokens[1] === "row"
? rotateRow(screen, rotateParams)
: rotateColumn(screen, rotateParams);
}
Insert cell
Insert cell
function setScreen(input) {
const screen = createScreen(50, 6);
input.split("\n").forEach((instr) => execute(screen, instr.split(" ")));
return screen;
}
Insert cell
function part1(input) {
return setScreen(input)
.flat()
.filter((pixel) => pixel === "█").length;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return AOC.ocr(setScreen(input).map((row) => row.join("")));
}
Insert cell
Insert cell
Insert cell
Insert cell
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