diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2020-12-04 10:13:16 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2020-12-04 10:13:16 +0200 |
| commit | 2a09118fcd1ebb62413291f1a0ec973339b73ed6 (patch) | |
| tree | fd1f318165f8f90fa4ee6f5bce1d9cf43b46ef49 /day4/index.ts | |
| parent | 983000000fdd92cee5029c2c2f94a607c9c31389 (diff) | |
Solve 4.1
Diffstat (limited to 'day4/index.ts')
| -rw-r--r-- | day4/index.ts | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/day4/index.ts b/day4/index.ts new file mode 100644 index 0000000..1d059dc --- /dev/null +++ b/day4/index.ts @@ -0,0 +1,52 @@ +import { Observable, of } from "rxjs"; +import { concatAll, count, filter, groupBy, map, mergeMap, reduce, tap } from 'rxjs/operators'; +import { ExerciseModuleFunc } from "../types"; + +interface Passport { + byr: string; + iyr: string; + eyr: string; + hgt: string; + hcl: string; + ecl: string; + pid: string; + cid?: string; +} + +const parsePassports = (input: string): Passport[] => { + const sections = input.split("\n\n") + const splitted = sections.map(s => s.split(/[\n\r\s]+/g)); + + const result = splitted.map(s => s.reduce((acc, val) => { + const [lhs, rhs] = val.split(":"); + acc[lhs] = rhs; + return acc; + }, {})) as Passport[]; + + return result; +} + +const isPassportValid = (passport: Passport): boolean => { + const requiredKeys = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]; + + let valid = true; + requiredKeys.forEach(key => { + if (passport[key] === undefined) { + valid = false; + } + }); + return valid; +} + +const day4: ExerciseModuleFunc = async (input: string) => { + const prom1 = of(input).pipe( + map(parsePassports), + concatAll(), + filter(isPassportValid), + count() + ).toPromise(); + + return Promise.all([prom1]); +} + +export default day4; |
