-
Notifications
You must be signed in to change notification settings - Fork 0
/
part-2.js
32 lines (28 loc) · 856 Bytes
/
part-2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
For each group, count the number of questions to which everyone answered "yes".
What is the sum of those counts?
*/
import readFile from '../read-file.js';
const answerGroups = [];
let nextGroup = [];
await readFile(new URL('./input.txt', import.meta.url), (line) => {
if (line.length === 0) {
answerGroups.push(nextGroup);
nextGroup = [];
} else {
nextGroup.push(line.split(''));
}
});
answerGroups.push(nextGroup);
// https://exploringjs.com/impatient-js/ch_sets.html#intersection-a-b
const intersection = (a, b) => {
const setA = new Set(a);
const setB = new Set(b);
return [...setA].filter((x) => setB.has(x));
};
const countIntersectionAnswers = (answerGroup) =>
answerGroup.reduce(intersection).length;
const result = answerGroups
.map(countIntersectionAnswers)
.reduce((acc, n) => acc + n);
console.log(result);