You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
586 B
JavaScript

4 years ago
const { readInput, sum } = require("./util");
const { paragraphs } = readInput();
function intersect(strA, strB) {
const setB = new Set(strB);
return strA
.split("")
.filter((el) => setB.has(el))
.join("");
}
function intersections(...strings) {
let res = strings[0];
for (const cur of strings.slice(1)) {
res = intersect(res, cur);
}
return res;
}
console.log("Part1:");
console.log(sum(paragraphs.map((p) => new Set(p.replaceAll("\n", "")).size)));
console.log("Part2:");
console.log(sum(paragraphs.map((p) => intersections(...p.split("\n")).length)));