aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/days/day05.gleam
blob: 076ae1dc8b39d75400ff537a7bac19cc6718f98d (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
import gleam/io
import gleam/int
import gleam/list
import gleam/set
import gleam/string as str
import gleam/iterator as iter
import ext/resultx as resx
import util/input_util

fn get_seat_id(pass: String) -> Int {
  pass
  |> str.to_graphemes
  |> list.map(with: fn(grapheme) {
    case grapheme {
      "F" | "L" -> "0"
      "B" | "R" -> "1"
      _ -> panic
    }
  })
  |> str.concat
  |> int.base_parse(2)
  |> resx.assert_unwrap
}

fn part1(lines: List(String)) -> Int {
  lines
  |> list.map(with: get_seat_id)
  |> list.reduce(with: int.max)
  |> resx.assert_unwrap
}

fn part2(lines: List(String)) -> Int {
  let seat_ids =
    lines
    |> list.map(with: get_seat_id)
    |> set.from_list

  let occupied = set.contains(in: seat_ids, this: _)

  iter.range(from: 1, to: 1023)
  |> iter.find(one_that: fn(id) {
    occupied(id - 1) && !occupied(id) && occupied(id + 1)
  })
  |> resx.assert_unwrap
}

pub fn main() -> Nil {
  let testing = input_util.read_lines("test05")
  let assert 820 = part1(testing)

  let input = input_util.read_lines("day05")
  io.debug(part1(input))
  io.debug(part2(input))

  Nil
}