aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/src/day2/solve.gleam
blob: 608955f0a2d9021ccea8c1751ebd62088469723b (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
import adglent.{First, Second}
import gleam/io
import gleam/int
import gleam/string
import gleam/list

pub type Game {
  Game(red: Int, blue: Int, green: Int)
}

fn parse(input: String) -> List(List(Game)) {
  use line <- list.map(string.split(input, "\n"))
  let assert [_, rounds] = string.split(line, on: ": ")
  use match <- list.map(string.split(rounds, on: "; "))
  use acc, draw <- list.fold(
    over: string.split(match, on: ", "),
    from: Game(0, 0, 0),
  )
  let assert Ok(#(n_str, color)) = string.split_once(draw, " ")
  let assert Ok(n) = int.parse(n_str)
  case color {
    "red" -> Game(..acc, red: n)
    "blue" -> Game(..acc, blue: n)
    "green" -> Game(..acc, green: n)
    _ -> panic as "unrecognized color"
  }
}

pub fn part1(input: String) {
  use acc, game, i <- list.index_fold(parse(input), 0)
  case list.any(game, fn(m) { m.red > 12 || m.green > 13 || m.blue > 14 }) {
    False -> acc + i + 1
    True -> acc
  }
}

pub fn part2(input: String) {
  {
    use game <- list.map(parse(input))
    use acc, match <- list.fold(game, Game(0, 0, 0))
    let Game(red: red, green: green, blue: blue) = match
    Game(
      red: int.max(red, acc.red),
      blue: int.max(blue, acc.blue),
      green: int.max(green, acc.green),
    )
  }
  |> list.fold(from: 0, with: fn(acc, g: Game) {
    acc + g.red * g.blue * g.green
  })
}

pub fn main() {
  let assert Ok(part) = adglent.get_part()
  let assert Ok(input) = adglent.get_input("2")
  case part {
    First ->
      part1(input)
      |> adglent.inspect
      |> io.println
    Second ->
      part2(input)
      |> adglent.inspect
      |> io.println
  }
}