From ba9af02ad7c56e055877facbe78e082f9c925d24 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 7 Dec 2020 12:58:47 +0200 Subject: Solve 6.2 --- day6/index.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'day6') 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 = (a: Set, b: Set) => + new Set(Array.from(a).filter(x => b.has(x))); + const getUniq = (group: Group): Set => { const joined = group.join("").split(""); const set = new Set(joined); return set; } +const getIntersection = (group: Group): Set => { + 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; -- cgit v1.3