Public
Edited
Dec 1, 2023
Insert cell
Insert cell
Insert cell
function parse(input) {
return [...input].map((c) => (c == "^" ? true : false));
}
Insert cell
Insert cell
function getNextRow(row) {
return row.map((_, i) => (row[i - 1] || false) !== (row[i + 1] || false));
}
Insert cell
Insert cell
function countSafe(row, numRows) {
let count = 0;
for (let i = 0; i < numRows; i++) {
count += row.reduce((acc, tile) => acc + (tile ? 0 : 1), 0);
row = getNextRow(row);
}
return count;
}
Insert cell
function part1(input) {
return countSafe(parse(input), 40);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return countSafe(parse(input), 400000);
}
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