aboutsummaryrefslogtreecommitdiff
path: root/aoc2017-gleam/src/aoc_2017/day_2.gleam
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-06-03 12:01:32 -0400
committerH.J <thechairman@thechairman.info>2024-06-03 12:01:32 -0400
commita679186bed8e5e284604fab7ef8ac932b66d4d51 (patch)
tree89cd10f2a7b350dad0c7fc94308e508e532db3a7 /aoc2017-gleam/src/aoc_2017/day_2.gleam
parent7d704786ec158349f2f34590a858df6e78e844d6 (diff)
downloadgleam_aoc-a679186bed8e5e284604fab7ef8ac932b66d4d51.tar.gz
gleam_aoc-a679186bed8e5e284604fab7ef8ac932b66d4d51.zip
gleam 2017 day 2 and 3
Diffstat (limited to 'aoc2017-gleam/src/aoc_2017/day_2.gleam')
-rw-r--r--aoc2017-gleam/src/aoc_2017/day_2.gleam47
1 files changed, 47 insertions, 0 deletions
diff --git a/aoc2017-gleam/src/aoc_2017/day_2.gleam b/aoc2017-gleam/src/aoc_2017/day_2.gleam
new file mode 100644
index 0000000..6a5e85d
--- /dev/null
+++ b/aoc2017-gleam/src/aoc_2017/day_2.gleam
@@ -0,0 +1,47 @@
+import gleam/int
+import gleam/list
+import gleam/result
+import gleam/string
+
+pub fn parse(input: String) {
+ use row <- list.map(string.split(input, "\n"))
+ use val <- list.map(string.split(row, "\t"))
+ let assert Ok(n) = int.parse(val)
+ n
+}
+
+pub fn pt_1(input: List(List(Int))) {
+ use acc, row <- list.fold(input, 0)
+ acc + max(row) - min(row)
+}
+
+pub fn pt_2(input: List(List(Int))) {
+ use acc, row <- list.fold(input, 0)
+ let assert [val] =
+ row |> list.combination_pairs() |> list.map(test_pair) |> result.values()
+ acc + val
+}
+
+fn max(xs) {
+ let assert Ok(result) = list.reduce(xs, int.max)
+ result
+}
+
+fn min(xs) {
+ let assert Ok(result) = list.reduce(xs, int.min)
+ result
+}
+
+fn test_pair(tup) {
+ case tup {
+ #(a, b) if a > b -> check_divisibility(a, b)
+ #(b, a) -> check_divisibility(a, b)
+ }
+}
+
+fn check_divisibility(a, b) {
+ case a % b {
+ 0 -> Ok(a / b)
+ _ -> Error(Nil)
+ }
+}