Public
Edited
Nov 11, 2022
1 star
Insert cell
Insert cell
Insert cell
function diffs(xs) {
const sorted = AOC.sort([0, ...xs]);
const ds = new Array(sorted.length - 1);
for (let i = 1; i < sorted.length; i++) {
ds[i - 1] = sorted[i] - sorted[i - 1];
}
return ds;
}
Insert cell
Insert cell
function part1(input) {
const ds = diffs(AOC.linesToNumbers(input));
return ds.filter(x => x == 1).length * (ds.filter(x => x == 3).length + 1);
}
Insert cell
Insert cell
Insert cell
Insert cell
function numPathSplits(xs) {
if (xs[0] == 1) {
switch (xs.length) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 4;
case 4:
return 7;
}
} else {
return 1;
}
}
Insert cell
Insert cell
function part2(input) {
return AOC.product(
AOC.group(diffs(AOC.linesToNumbers(input))).map(numPathSplits)
);
}
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