Public
Edited
Oct 1, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function binaryHash(key, row) {
const lengths = [...`${key}-${row}`]
.map((c) => c.charCodeAt(0))
.concat([17, 31, 73, 47, 23]);
return [...denseHash(knotHash(lengths))].map(AOC.hexToBinary).join("");
}
Insert cell
Insert cell
function countUsedSquares(key) {
let used = 0;
for (let row = 0; row < 128; row++) {
used += [...binaryHash(key, row)].reduce((acc, b) => acc + (b === "1"), 0);
}
return used;
}
Insert cell
function part1(input) {
return countUsedSquares(input);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function dfs(grid, x, y) {
if (x < 0 || x >= 128 || y < 0 || y >= 128 || grid[x][y] === "0") {
return;
}
grid[x][y] = "0"; // Mark cell as visited
dfs(grid, x - 1, y);
dfs(grid, x + 1, y);
dfs(grid, x, y - 1);
dfs(grid, x, y + 1);
}
Insert cell
Insert cell
function countRegions(key) {
const grid = [...Array(128)].map((_, row) => [...binaryHash(key, row)]);
let numRegions = 0;
for (let x = 0; x < 128; x++) {
for (let y = 0; y < 128; y++) {
if (grid[x][y] === "1") {
dfs(grid, x, y);
numRegions++;
}
}
}
return numRegions;
}
Insert cell
function part2(input) {
return countRegions(input);
}
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