Public
Edited
Dec 8, 2022
Importers
Insert cell
Insert cell
Insert cell
Insert cell
parse = aoc.lines((l) => l.split(""))
Insert cell
letterValue = (c) => {
const v = c.charCodeAt(0);
if (v >= 97) return v - 97 + 1;
else return v - 65 + 27;
}
Insert cell
function part1(input) {
let values = input.map((d) => {
let half1 = d.slice(0, d.length / 2);
let half2 = new Set(d.slice(d.length / 2));
for (let l of half1) {
if (half2.has(l)) return letterValue(l);
}
return { half1, half2 };
throw new Error("no dupe found");
});
return d3.sum(values);
}
Insert cell
function part2(input) {
let badges = [];
for (let i = 0; i < input.length - 2; i += 3) {
let bag1 = input[i];
let bag2 = new Set(input[i + 1]);
let bag3 = new Set(input[i + 2]);
for (const l of bag1) {
if (bag2.has(l) && bag3.has(l)) {
badges.push(l);
break;
}
}
}
return d3.sum(badges, letterValue);
}
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