-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
33 lines (29 loc) · 817 Bytes
/
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
function union<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set([...a, ...b]);
}
function intersection<T>(a: Set<T>, b: Set<T>): Set<T> {
return new Set([...a].filter((element) => b.has(element)));
}
function part1(groups: string[]): number {
return groups.reduce(
(totalGroups, group) =>
totalGroups +
group
.split('\n')
.map((person) => new Set(person))
.reduce((totalPeople, person) => union(totalPeople, new Set(person))).size,
0,
);
}
function part2(groups: string[]): number {
return groups.reduce(
(totalGroups, group) =>
totalGroups +
group
.split('\n')
.map((person) => new Set(person))
.reduce((totalPeople, person) => intersection(totalPeople, new Set(person))).size,
0,
);
}
export { part1, part2 };