aboutsummaryrefslogtreecommitdiff
path: root/aoc2017-gleam/src/aoc_2017/day_4.gleam
blob: 9bc4f9aa4a4ae52a50b4f1eb70ae5f62392aea9c (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
import gleam/list
import gleam/string

pub fn parse(input: String) -> List(List(String)) {
  use row <- list.map(string.split(input, "\n"))
  string.split(row, " ")
}

pub fn pt_1(input: List(List(String))) {
  use acc, passwords <- list.fold(input, 0)
  case passwords == list.unique(passwords) {
    True -> acc + 1
    False -> acc
  }
}

pub fn pt_2(input: List(List(String))) {
  use acc, passwords <- list.fold(input, 0)
  let sorted_passwords = list.map(passwords, sort_graphemes)
  case sorted_passwords == list.unique(sorted_passwords) {
    True -> acc + 1
    False -> acc
  }
}

fn sort_graphemes(word: String) -> String {
  word
  |> string.to_graphemes
  |> list.sort(string.compare)
  |> string.concat
}