diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2020-12-02 09:50:42 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2020-12-02 09:50:42 +0200 |
| commit | 9612a3cedba1b4fb687ecd3928a2f56d841ded26 (patch) | |
| tree | 4ddf05e66609aa72f3d1239dc452969a152a936a /day2/index.ts | |
| parent | cca3701cd6bb6c479aeb1ece71aa5bae5383ae65 (diff) | |
Solve 2.1
Diffstat (limited to 'day2/index.ts')
| -rw-r--r-- | day2/index.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/day2/index.ts b/day2/index.ts new file mode 100644 index 0000000..e42e005 --- /dev/null +++ b/day2/index.ts @@ -0,0 +1,47 @@ +import { of } from "rxjs"; +import { concatAll, count, filter, map, take } from 'rxjs/operators'; +import { ExerciseModuleFunc } from "../types"; + +interface Row { + min: number; + max: number; + letter: string; + pwd: string; +} + +const parse = (line: string): Row => { + const re = /(\d+)-(\d+) (\w{1}): (\w+)/; + const match = line.match(re); + + return { + min: Number(match[1]), + max: Number(match[2]), + letter: match[3], + pwd: match[4] + } +} + +const isValid = (r: Row): boolean => { + let count = 0; + r.pwd.split("").forEach(char => { + if (char === r.letter) { + count += 1; + } + }); + return count >= r.min && count <= r.max; +} + +const day2: ExerciseModuleFunc = async (input: string) => { + const lines = input.split("\n"); + + const prom1 = of(lines).pipe( + concatAll(), + map(parse), + filter(isValid), + count() + ).toPromise(); + + return [await prom1]; +} + +export default day2; |
