-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
46 lines (37 loc) · 1.02 KB
/
index.ts
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
export const part1 = (input: string): number => {
const groups = input.split("\n\n");
const answersPerGroup = groups.map((str) => str.split(/\s?/));
return answersPerGroup.reduce(
(sum, answers) => sum + new Set(answers).size,
0
);
};
const getLargerList = (lists: Array<any[]>): any[] => {
let largerList = [];
for (let list of lists) {
if (list.length > largerList.length) {
largerList = list;
}
}
return largerList;
};
const intersection = (...lists: Array<string[]>): string[] => {
const largerList: string[] = getLargerList(lists);
const intersectionValues: string[] = [];
for (let value of largerList) {
if (lists.every((val) => val.includes(value))) {
intersectionValues.push(value);
}
}
return intersectionValues;
};
export const part2 = (input: string) => {
const groups = input.split("\n\n");
return groups.reduce(
(sum, group) =>
sum +
intersection(...group.split("\n").map((answers) => answers.split("")))
.length,
0
);
};