Public
Edited
Dec 2, 2024
Paused
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function splitEvenly(lines) {
return lines.map((l) => [l.slice(0, l.length / 2), l.slice(l.length / 2)]);
}
Insert cell
Insert cell
function common([left, right]) {
return [...AOC.intersection(new Set([...left]), new Set([...right]))][0];
}
Insert cell
Insert cell
function priority(item) {
return item < "a" ? item.charCodeAt(0) - 38 : item.charCodeAt(0) - 96;
}
Insert cell
Insert cell
function part1(input) {
return AOC.sum(
splitEvenly(input.split("\n")).map((items) => priority(common(items)))
);
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const grps = AOC.chunk(input.split("\n"), 3).map((grp) =>
grp.map((rucksack) => new Set(rucksack))
);
return AOC.sum(grps.map((grp) => priority([...AOC.intersections(grp)][0])));
}
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