advent-of-code/2020/06/solution1.js

24 lines
586 B
JavaScript
Raw Normal View History

2020-12-07 07:00:56 +01:00
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)));