aboutsummaryrefslogtreecommitdiffstats
path: root/day6/index.ts
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2020-12-07 12:58:47 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2020-12-07 12:58:47 +0200
commitba9af02ad7c56e055877facbe78e082f9c925d24 (patch)
tree65a31c2a893a5ecc863e07c57358b74aa4e2872e /day6/index.ts
parent239d4e44f6e8bc49aedea6462eb12c7a9742811f (diff)
Solve 6.2
Diffstat (limited to 'day6/index.ts')
-rw-r--r--day6/index.ts21
1 files changed, 20 insertions, 1 deletions
diff --git a/day6/index.ts b/day6/index.ts
index 612aebf..1b9e69f 100644
--- a/day6/index.ts
+++ b/day6/index.ts
@@ -4,12 +4,24 @@ import { ExerciseModuleFunc } from "../types";
type Group = string[];
+const intersection = <T extends unknown>(a: Set<T>, b: Set<T>) =>
+ new Set(Array.from(a).filter(x => b.has(x)));
+
const getUniq = (group: Group): Set<string> => {
const joined = group.join("").split("");
const set = new Set(joined);
return set;
}
+const getIntersection = (group: Group): Set<string> => {
+ let result = new Set(group[0]);
+ for (let i = 1; i < group.length; i += 1) {
+ const set = new Set(group[i]);
+ result = intersection(result, set);
+ }
+ return result;
+}
+
const day6: ExerciseModuleFunc = async (input: string) => {
const splitted = input.split("\n\n");
const groups: Group[] = splitted.map(g => g.split("\n"));
@@ -23,7 +35,14 @@ const day6: ExerciseModuleFunc = async (input: string) => {
reduce((acc, val) => acc + val)
).toPromise();
- return Promise.all([prom1]);
+ const prom2 = of(groups).pipe(
+ concatAll(),
+ map(getIntersection),
+ map(res => res.size),
+ reduce((acc, val) => acc + val)
+ ).toPromise();
+
+ return Promise.all([prom1, prom2]);
}
export default day6;