aboutsummaryrefslogtreecommitdiff
path: root/aoc2017-gleam/src/aoc_2017/day_9.gleam
blob: 90eb4b37d3fc9857b84fcb314892a53ab10168e7 (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
import gleam/list
import gleam/option.{Some}
import gleam/regex
import gleam/string

pub fn parse(input: String) {
  let assert Ok(cancel) = regex.from_string("!.")

  replace(input, with: cancel)
}

pub fn pt_1(input: String) {
  input
  |> strip_to_brackets()
  |> next_bracket(0, 1)
}

fn replace(input: String, with regex: regex.Regex) -> String {
  input |> regex.split(with: regex) |> string.concat()
}

fn strip_to_brackets(input: String) -> String {
  let assert Ok(garbage) = regex.from_string("<.*?>")
  let assert Ok(not_group) = regex.from_string("[^{}]")

  input
  |> replace(with: garbage)
  |> replace(with: not_group)
}

fn next_bracket(brackets: String, score: Int, depth: Int) -> Int {
  case string.pop_grapheme(brackets) {
    Error(Nil) -> score
    Ok(#("{", rest)) -> next_bracket(rest, score + depth, depth + 1)
    Ok(#("}", rest)) -> next_bracket(rest, score, depth - 1)
    _ -> panic as "unrecognized character"
  }
}

pub fn pt_2(input: String) {
  let assert Ok(garbage) = regex.from_string("<(.*?)>")

  use acc, match <- list.fold(regex.scan(input, with: garbage), 0)
  case match.submatches {
    [Some(g)] -> string.length(g) + acc
    _ -> acc
  }
}