aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/days/day04.gleam
blob: 6087e12779dafed1c2d07d51a1821698383962c3 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import gleam/io
import gleam/list
import gleam/function as fun
import gleam/result as res
import gleam/map.{Map}
import ext/listx
import ext/resultx as resx
import util/input_util
import util/parser as p

const allowed_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid"]

const required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]

const eye_colors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]

type Passport {
  Passport(fields: Map(String, String))
}

fn parse_passports(from text: String) -> List(Passport) {
  let key_parser =
    p.any_str_of_len(3)
    |> p.labeled(with: "key")
  let value_parser =
    p.str1_until_ws()
    |> p.labeled(with: "value")
  let field_parser =
    key_parser
    |> p.then_skip(p.literal(":"))
    |> p.then(value_parser)
    |> p.labeled(with: "field")
  let passport_parser =
    field_parser
    |> p.sep1(by: p.ws_gc())
    |> p.map(with: fun.compose(map.from_list, Passport))
    |> p.labeled(with: "passport")
  let input_parser =
    passport_parser
    |> p.sep1(by: p.literal("\n\n"))
    |> p.then_skip(p.opt(p.ws_gc()))
    |> p.labeled(with: "input")

  text
  |> p.parse_entire(with: input_parser)
  |> resx.assert_unwrap
}

fn is_valid1(passport: Passport) -> Bool {
  let has_only_allowed_keys =
    map.keys(passport.fields)
    |> list.all(satisfying: list.contains(allowed_fields, _))

  let has_all_required_keys =
    required_fields
    |> list.all(satisfying: list.contains(map.keys(passport.fields), _))

  has_only_allowed_keys && has_all_required_keys
}

fn is_valid2(passport: Passport) -> Bool {
  let int_between = fn(min, max) {
    p.int()
    |> p.satisfying(rule: fn(number) { min <= number && number <= max })
    |> p.ignore
  }

  let validators = [
    #("byr", int_between(1920, 2002)),
    #("iyr", int_between(2010, 2020)),
    #("eyr", int_between(2020, 2030)),
    #(
      "hgt",
      p.or(
        int_between(150, 193)
        |> p.then(p.literal("cm")),
        int_between(59, 76)
        |> p.then(p.literal("in")),
      )
      |> p.ignore,
    ),
    #(
      "hcl",
      p.literal("#")
      |> p.then(
        p.gc_in(range: "0123456789abcdef")
        |> p.repeat(times: 6),
      )
      |> p.ignore,
    ),
    #(
      "ecl",
      eye_colors
      |> list.map(with: p.literal)
      |> p.any
      |> p.ignore,
    ),
    #(
      "pid",
      p.digit()
      |> p.str_of_len(9)
      |> p.ignore,
    ),
  ]

  is_valid1(passport) && list.all(
    validators,
    satisfying: fn(validator) {
      let #(key, parser) = validator
      passport.fields
      |> map.get(key)
      |> res.then(apply: fn(value) {
        value
        |> p.parse_entire(with: parser)
        |> res.replace_error(Nil)
      })
      |> res.is_ok
    },
  )
}

fn part1(text: String) -> Int {
  text
  |> parse_passports
  |> listx.count(satisfying: is_valid1)
}

fn part2(text: String) -> Int {
  text
  |> parse_passports
  |> listx.count(satisfying: is_valid2)
}

pub fn run() -> Nil {
  let test = input_util.read_text("test04")
  assert 2 = part1(test)
  assert 2 = part2(test)

  let input = input_util.read_text("day04")
  io.debug(part1(input))
  io.debug(part2(input))

  Nil
}