Public
Edited
Dec 6, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
totalContents = input.trim().split("\n");
Insert cell
function getSackOutlier(contents) {
const {length} = contents;
const halfLength = length / 2;
const comp1 = contents.slice(0, halfLength);
const comp2 = contents.slice(halfLength);
const items1 = new Set();
const items2 = new Set();

for (let i = 0; i < halfLength; i++) {
if (items2.has(comp1[i])) return comp1[i]
else items1.add(comp1[i]);
if (items1.has(comp2[i])) return comp2[i]
else items2.add(comp2[i]);
}
throw new Error("No matches found!");
}
Insert cell
sackOutliers = totalContents.map(contents => getSackOutlier(contents));
Insert cell
function getPriority(letter) {
const code = letter.charCodeAt(0);
return code < 91
? code - 38
: code - 96;
}
Insert cell
priorities = sackOutliers.map(outlier => getPriority(outlier));
Insert cell
totalPriorities = priorities.reduce((total, p) => total + p)
Insert cell
Insert cell
elfGroups = {
const groups = [];
for (let i = 0; i <= totalContents.length - 3; i += 3) {
groups.push(totalContents.slice(i, i + 3))
}

return groups;
}
Insert cell
function getGroupBadge([group1, group2, group3]) {
const items1 = new Set(group1);
const items2 = new Set(group2);
for (const letter of group3) {
if (items1.has(letter) && items2.has(letter)) return letter;
}

throw new Error("No badge found!");
}
Insert cell
groupBadges = elfGroups.map(group => getGroupBadge(group));
Insert cell
totalBadgePriorities = groupBadges.reduce((total, p) => total + getPriority(p), 0)
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