Public
Edited
Dec 8, 2022
1 fork
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
tidyData = parse(inputs.real).flatMap((row, x) =>
row.flatMap((height, y) => ({ x, y, height }))
)
Insert cell
Insert cell
parse = aoc.lines((l) => l.split("").map(Number))
Insert cell
function part1(input) {
let isVisible = new Set();
for (let x = 0; x < input.length; x++) {
// dir 1
let max = -Infinity;
for (let y = 0; y < input[x].length; y++) {
if (input[x][y] > max) {
max = input[x][y];
isVisible.add(`${x},${y}`);
}
}
// dir 2
max = -Infinity;
for (let y = input[x].length - 1; y >= 0; y--) {
if (input[x][y] > max) {
max = input[x][y];
isVisible.add(`${x},${y}`);
}
}
}

for (let y = 0; y < input[0].length; y++) {
// dir 3
let max = -Infinity;
for (let x = 0; x < input.length; x++) {
if (input[x][y] > max) {
max = input[x][y];
isVisible.add(`${x},${y}`);
}
}
// dir 4
max = -Infinity;
for (let x = input.length - 1; x >= 0; x--) {
if (input[x][y] > max) {
max = input[x][y];
isVisible.add(`${x},${y}`);
}
}
}

return isVisible.size;
}
Insert cell
function scenicOneDir(input, x, y, dx, dy) {
let start = input[x][y];
let score = 0;
let cx = x + dx;
let cy = y + dy;
for (
let [cx, cy] = [x + dx, y + dy];
cx < input.length && cx >= 0 && cy < input[0].length && cy >= 0;
[cx, cy] = [cx + dx, cy + dy]
) {
score += 1;
if (input[cx][cy] >= start) break;
}
return score;
}
Insert cell
function part2(input) {
let scores = [];
for (let x = 3; x < input.length; x++) {
for (let y = 2; y < input[x].length; y++) {
let dir1 = scenicOneDir(input, x, y, -1, 0);
let dir2 = scenicOneDir(input, x, y, 0, -1);
let dir3 = scenicOneDir(input, x, y, 1, 0);
let dir4 = scenicOneDir(input, x, y, 0, 1);
scores.push(dir1 * dir2 * dir3 * dir4);
}
}
return d3.max(scores);
}
Insert cell
Insert cell
meta = aoc.meta({
day: 8,
parse,
inputs,
parts: [part1, part2],
expected: { test: [21, 8], real: [1827, 335580] },
href: "https://observablehq.com/d/abeaee4ec71b9690?collection=@mythmon/advent-of-code-2022"
})
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