diff options
Diffstat (limited to 'day6')
| -rw-r--r-- | day6/index.ts | 21 |
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; |
