Public
Edited
Nov 27, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function toLayers(input) {
const numLayers = input.length / (numRows * numCols);
return AOC.chunk(input, numCols * numRows).map((layer) =>
AOC.chunk(layer, numCols)
);
}
Insert cell
Insert cell
function numOfDigit(n, layer) {
return layer.flat().filter((c) => c === n).length;
}
Insert cell
Insert cell
function minZeroLayer(input) {
let minZeroCount = 9999;
let minLayer;
toLayers(input).forEach((layer) => {
const zeroCount = numOfDigit(0, layer);
if (zeroCount < minZeroCount) {
minZeroCount = zeroCount;
minLayer = layer;
}
});
return minLayer;
}
Insert cell
Insert cell
function part1(input) {
const layer = minZeroLayer(input);
return numOfDigit(1, layer) * numOfDigit(2, layer);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function combineGrids(layers) {
const grid = AOC.gInit(numRows, numCols, 0);
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
let colour = 2;
for (let l = layers.length - 1; l >= 0; l--) {
colour = layers[l][row][col] == 2 ? colour : layers[l][row][col];
}
grid[row][col] = colour;
}
}
return grid;
}
Insert cell
Insert cell
function drawGrids(grid) {
const sc = 20;
const r = new Renderer(sc * numCols, sc * numRows).fit([
[-1, -1],
[numCols + 1, numRows + 1]
]);

grid.flat().forEach((c, i) =>
c === 1 ? r.ellipse(i % numCols, Math.floor(i / numCols), 0.5) : r
);
return r.render();
}
Insert cell
drawGrids(combineGrids(toLayers(puzzleInput)))
Insert cell
function part2(input) {
return "KCGEC";
}
Insert cell
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