From 23c7ca166ac87a339c6ee6ed71aed41afefb5513 Mon Sep 17 00:00:00 2001 From: "H.J" Date: Mon, 3 Jun 2024 17:16:30 -0400 Subject: gleam 2017 up to day 7 part 1 --- aoc2017-gleam/src/aoc_2017/day_4.gleam | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 aoc2017-gleam/src/aoc_2017/day_4.gleam (limited to 'aoc2017-gleam/src/aoc_2017/day_4.gleam') diff --git a/aoc2017-gleam/src/aoc_2017/day_4.gleam b/aoc2017-gleam/src/aoc_2017/day_4.gleam new file mode 100644 index 0000000..9bc4f9a --- /dev/null +++ b/aoc2017-gleam/src/aoc_2017/day_4.gleam @@ -0,0 +1,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 +} -- cgit v1.2.3