Public
Edited
Dec 3, 2022
Insert cell
Insert cell
Insert cell
parse = (input) =>
input
.split("\n")
.map((r) => [r.slice(0, r.length / 2), r.slice(r.length / 2)])
Insert cell
parse(testInput)
Insert cell
priority = (char) =>
char.charCodeAt(0) <= 90 ? char.charCodeAt(0) - 38 : char.charCodeAt(0) - 96
Insert cell
[priority("a"), priority("z"), priority("A"), priority("Z")]
Insert cell
findDupe = (a, b) => {
for (let i = 0; i < a.length; i++) {
const needle = a[i];
for (let j = 0; j < b.length; j++) {
if (b[j] === needle) return b[j];
}
}
}
Insert cell
parse(testInput).map((r) => findDupe(r[0], r[1]))
Insert cell
sum = (arr) => arr.reduce((acc, n) => acc + n)
Insert cell
part1 = (input) =>
sum(
parse(input)
.map(([a, b]) => findDupe(a, b))
.map(priority)
)
Insert cell
part1(testInput)
Insert cell
Insert cell
part1(input)
Insert cell
group = (rucksacks) => {
let groups = [];
for (let i = 0; i < rucksacks.length; i += 3) {
groups.push([rucksacks[i], rucksacks[i + 1], rucksacks[i + 2]]);
}
return groups;
}
Insert cell
group(testInput.split("\n"))
Insert cell
unique = (arr) => [...new Set(arr)]
Insert cell
unique(["a", "a", "b", "a"])
Insert cell
findCommon = (rucksacks) => {
const itemCount = rucksacks.reduce(
(found, sack) =>
unique(sack.split("")).reduce((found2, item) => {
found2[item] ? found2[item]++ : (found2[item] = 1);
return found;
}, found),
{}
);
//return itemCount;
return Object.keys(itemCount).find((item) => itemCount[item] === 3);
}
Insert cell
findCommon([
"vJrwpWtwJgWrhcsFMMfFFhFp",
"jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL",
"PmmdzqPrVvPwwTWBwg"
])

Insert cell
findCommon([
"wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn",
"ttgJtRGJQctTZtZT",
"CrZsJsPPZsGzwwsLwLmpwMDw"
])
Insert cell
part2 = (input) => sum(group(input.split("\n")).map(findCommon).map(priority))
Insert cell
part2(testInput)
Insert cell
part2(input)
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