Public
Edited
Dec 8
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input
.split("\n")
.map((d) => d.split(/\s+/).map(Number))
.map((d) => (d[0] > d.at(-1) ? d : d.reverse()));
}
Insert cell
Insert cell
function isSafe(rep) {
return rep.every(
(_, i) => i === 0 || (rep[i - 1] - rep[i] <= 3 && rep[i - 1] - rep[i] >= 1)
);
}
Insert cell
Insert cell
function part1(input) {
return parse(input).filter(isSafe).length;
}
Insert cell
Insert cell
Insert cell
Insert cell
function isSafe2(rep) {
return isSafe(rep) ? true : AOC.select(rep).some((d) => isSafe(d[1]));
}
Insert cell
function part2(input) {
return parse(input).filter(isSafe2).length;
}
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