commit 171079922515fa79e2e960ecbeac9f1d70ccdf97 Author: Hamcha Date: Tue Dec 3 20:07:34 2024 +0100 day1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30faab5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.beam +*.ez +/build +erl_crash.dump +/ex/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..2ad637d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Advent of Code stuff diff --git a/gleam.toml b/gleam.toml new file mode 100644 index 0000000..60645d4 --- /dev/null +++ b/gleam.toml @@ -0,0 +1,10 @@ +name = "aoc" +version = "1.0.0" + +[dependencies] +gleam_stdlib = ">= 0.34.0 and < 2.0.0" +stdin = ">= 1.1.4 and < 2.0.0" +gleam_yielder = ">= 1.1.0 and < 2.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..edbf33a --- /dev/null +++ b/manifest.toml @@ -0,0 +1,15 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" }, + { name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" }, + { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, + { name = "stdin", version = "1.1.4", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "stdin", source = "hex", outer_checksum = "04C04035F2A4CEEFB023837249649CD25F9A9CF5A45F9947C4D0462428A4677D" }, +] + +[requirements] +gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } +gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +stdin = { version = ">= 1.1.4 and < 2.0.0" } diff --git a/sample/day1-sample.txt b/sample/day1-sample.txt new file mode 100644 index 0000000..fe29f8f --- /dev/null +++ b/sample/day1-sample.txt @@ -0,0 +1,7 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 + diff --git a/src/day1_p1.gleam b/src/day1_p1.gleam new file mode 100644 index 0000000..9440c7e --- /dev/null +++ b/src/day1_p1.gleam @@ -0,0 +1,42 @@ +import gleam/int +import gleam/io +import gleam/iterator +import gleam/list.{filter_map} +import gleam/string +import stdin.{stdin} + +pub fn main() { + stdin() + |> parse + |> sort + |> calculate_distance + |> io.debug +} + +fn calculate_distance(list: #(List(Int), List(Int))) -> Int { + let #(first, second) = list + list.map2(first, second, fn(first, second) { + int.absolute_value(second - first) + }) + |> list.fold(0, int.add) +} + +fn sort(list: #(List(Int), List(Int))) -> #(List(Int), List(Int)) { + let #(first, second) = list + #(list.sort(first, by: int.compare), list.sort(second, by: int.compare)) +} + +fn parse(it: iterator.Iterator(String)) -> #(List(Int), List(Int)) { + it + |> iterator.fold(#([], []), fn(acc, line) { + case string.trim(line) { + "" -> acc + str -> { + let #(first_list, second_list) = acc + let assert [first, second] = + str |> string.split(" ") |> filter_map(int.parse) + #([first, ..first_list], [second, ..second_list]) + } + } + }) +} diff --git a/src/day1_p2.gleam b/src/day1_p2.gleam new file mode 100644 index 0000000..ebe93cd --- /dev/null +++ b/src/day1_p2.gleam @@ -0,0 +1,36 @@ +import gleam/int +import gleam/io +import gleam/iterator +import gleam/list.{filter_map} +import gleam/string +import stdin.{stdin} + +pub fn main() { + stdin() + |> parse + |> calculate_similarity + |> io.debug +} + +fn calculate_similarity(list: #(List(Int), List(Int))) -> Int { + let #(first, second) = list + first + |> list.fold(0, fn(acc, elem) { + acc + elem * list.count(second, fn(candidate) { candidate == elem }) + }) +} + +fn parse(it: iterator.Iterator(String)) -> #(List(Int), List(Int)) { + it + |> iterator.fold(#([], []), fn(acc, line) { + case string.trim(line) { + "" -> acc + str -> { + let #(first_list, second_list) = acc + let assert [first, second] = + str |> string.split(" ") |> filter_map(int.parse) + #([first, ..first_list], [second, ..second_list]) + } + } + }) +}