Public
Edited
Dec 17, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sampleMatrix = parseInput(sampleInput)
Insert cell
puzzleAnswer2 = (matrix) => {
const [m, n] = size(matrix);

const score = matrix.map((row, i) =>
row.map((cell, j) => scenicScore(matrix, i, j))
);

return _.max(score.map(_.max));
}
Insert cell
scenicScore(sampleMatrix, 1, 2)
Insert cell
top(sampleMatrix, 1, 2)
Insert cell
left(sampleMatrix, 1, 2)
Insert cell
[3].findIndex((d) => d >= 5)
Insert cell
sampleMatrix[1][2]
Insert cell
scenicScore = (matrix, i, j) => {
const distToValue = (list, v) => {
const idx = list.findIndex((d) => d >= v);
return idx >= 0 ? idx + 1 : list.length;
};

const cell = matrix[i][j];
const distT = distToValue(top(matrix, i, j), cell);
const distD = distToValue(bottom(matrix, i, j), cell);
const distL = distToValue(left(matrix, i, j), cell);
const distR = distToValue(right(matrix, i, j), cell);

return product([distT, distD, distL, distR]);
}
Insert cell
function isVisible(matrix, row, col) {
const [m, n] = size(matrix);
if (row === 0 || row === m - 1 || col === 0 || col === n - 1) {
return 1;
}

const cell = matrix[row][col];
const visibleT = cell > _.max(top(matrix, row, col));
const visibleB = cell > _.max(bottom(matrix, row, col));
const visibleL = cell > _.max(left(matrix, row, col));
const visibleR = cell > _.max(right(matrix, row, col));

return visibleT || visibleB || visibleR || visibleL ? 1 : 0;
}
Insert cell
left = (matrix, row, col) =>
_.range(0, col)
.map((c) => matrix[row][c])
.reverse()
Insert cell
Insert cell
bottom = (matrix, row, col) =>
_.range(row + 1, matrix.length).map((r) => matrix[r][col])
Insert cell
top = (matrix, row, col) => _.range(0, row).map((r) => matrix[r][col]).reverse()
Insert cell
parseInput = (inputText) => {
const rowsText = inputText
.split(NEWLINE)
.map((row) => row.split("").map((d) => +d));
return rowsText;
}
Insert cell
Insert cell
product = (list) => list.reduce((a, b) => a * b, 1)
Insert cell
import { NEWLINE } from "@pnavarrc/advent-of-code-utils"
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