Public
Edited
Dec 1, 2023
Insert cell
Insert cell
Insert cell
function process(instrs) {
const grid = AOC.gInit(1000, 1000, false);
const regex = /(toggle|turn on|turn off) (\d+),(\d+) through (\d+),(\d+)/;

for (const instr of instrs) {
const match = instr.match(regex);
const [action, c0, r0, c1, r1] = [match[1], ...match.slice(2).map(Number)];
for (let r = r0; r <= r1; r++) {
for (let c = c0; c <= c1; c++) {
grid[r][c] = action === "toggle" ? !grid[r][c] : action === "turn on";
}
}
}
return grid.flat().filter((light) => light).length;
}
Insert cell
function part1(input) {
return process(input.split("\n"));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function processBrightness(instrs) {
const grid = AOC.gInit(1000, 1000, 0);
const regex = /(toggle|turn on|turn off) (\d+),(\d+) through (\d+),(\d+)/;

for (const instr of instrs) {
const match = instr.match(regex);
const [action, c0, r0, c1, r1] = [match[1], ...match.slice(2).map(Number)];
for (let r = r0; r <= r1; r++) {
for (let c = c0; c <= c1; c++) {
grid[r][c] =
action === "turn on"
? grid[r][c] + 1
: action === "turn off"
? Math.max(0, grid[r][c] - 1)
: grid[r][c] + 2;
}
}
}
return AOC.sum(grid.flat());
}
Insert cell
function part2(input) {
return processBrightness(input.split("\n"));
}
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