aboutsummaryrefslogtreecommitdiffstats
path: root/day4/index.ts
blob: 1d059dc32edb2d9630126cfa7fe69639fa37eb15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;