Public
Edited
Dec 8
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
const grid = input.split("\n").map((line) => line.split(""));
const nodes = grid.reduce((map, gRow, row) => {
gRow.forEach((type, col) => {
if (type !== ".") {
map.set(type, [...(map.get(type) || []), { row, col }]);
}
});
return map;
}, new Map());
return { nodes, nRows: grid.length, nCols: grid[0].length };
}
Insert cell
Insert cell
function findAntinodes(antennae, displacements = [2]) {
const antinodes = [];
const inBounds = ([r, c]) =>
r >= 0 && r < antennae.nRows && c >= 0 && c < antennae.nCols;

antennae.nodes.forEach((locations) => {
AOC.select(locations).forEach(([n, others]) => {
others.forEach((other) => {
const v = [other.row - n.row, other.col - n.col];
antinodes.push(
AOC.mapWhile(
(d) => [n.row + d * v[0], n.col + d * v[1]],
inBounds,
displacements
)
);
});
});
});
return antinodes.flat();
}
Insert cell
Insert cell
function part1(input) {
const nodes = parse(input);
const antinodes = findAntinodes(nodes);
return new Set(antinodes.map(([r, c]) => r * nodes.nCols + c)).size;
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const nodes = parse(input);
const antinodes = findAntinodes(nodes, d3.range(1, nodes.nRows));
return new Set(antinodes.map(([r, c]) => r * nodes.nCols + c)).size;
}
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