diff --git a/gleam.toml b/gleam.toml index 9f868f7..f157aa6 100644 --- a/gleam.toml +++ b/gleam.toml @@ -15,6 +15,7 @@ gleam_stdlib = "~> 0.39" gleam_json = "~> 2.0" gladvent = "~> 2.0" gleam_yielder = ">= 1.0.0 and < 2.0.0" +tote = ">= 1.0.2 and < 2.0.0" [dev-dependencies] gleeunit = ">= 1.2.0 and < 2.0.0" @@ -53,3 +54,6 @@ gleeunit = ">= 1.2.0 and < 2.0.0" [gladvent.2023] 1 = { pt_1 = 55123, pt_2 = 55260 } + +[gladvent.2024] +1 = { pt_1 = 1579939, pt_2 = 20351745 } diff --git a/manifest.toml b/manifest.toml index 6d03c5f..353852b 100644 --- a/manifest.toml +++ b/manifest.toml @@ -24,6 +24,7 @@ packages = [ { name = "snag", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "08E9EB87C413457DB1DD66CD704C6878DACC9C93B418600F63873D0CD224E756" }, { name = "spinner", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib", "glearray", "repeatedly"], otp_app = "spinner", source = "hex", outer_checksum = "9EE43AA33BE2DA5731B8F3F170AAB59AF1A815AFA5BF615F12C1B91F3B03F157" }, { name = "tom", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "228E667239504B57AD05EC3C332C930391592F6C974D0EFECF32FFD0F3629A27" }, + { name = "tote", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tote", source = "hex", outer_checksum = "A249892E26A53C668897F8D47845B0007EEE07707A1A03437487F0CD5A452CA5" }, ] [requirements] @@ -32,3 +33,4 @@ gleam_json = { version = "~> 2.0" } gleam_stdlib = { version = "~> 0.39" } gleam_yielder = { version = ">= 1.0.0 and < 2.0.0" } gleeunit = { version = ">= 1.2.0 and < 2.0.0" } +tote = { version = ">= 1.0.2 and < 2.0.0" } diff --git a/src/aoc_2024/day_1.gleam b/src/aoc_2024/day_1.gleam new file mode 100644 index 0000000..c41dceb --- /dev/null +++ b/src/aoc_2024/day_1.gleam @@ -0,0 +1,29 @@ +import gleam/int +import gleam/list +import gleam/string +import tote/bag + +pub fn parse(input: String) -> #(List(Int), List(Int)) { + let rows = { + use line <- list.map(string.split(input, "\n")) + let assert Ok(#(a, b)) = string.split_once(line, " ") + let assert Ok(left) = int.parse(a) + let assert Ok(right) = int.parse(b) + #(left, right) + } + + list.unzip(rows) +} + +pub fn pt_1(input: #(List(Int), List(Int))) -> Int { + let sort_ints = list.sort(_, int.compare) + let sorted = list.zip(sort_ints(input.0), sort_ints(input.1)) + use acc, #(left, right) <- list.fold(sorted, 0) + acc + int.absolute_value(left - right) +} + +pub fn pt_2(input: #(List(Int), List(Int))) { + let counts = bag.from_list(input.1) + use acc, id <- list.fold(input.0, 0) + acc + id * bag.copies(counts, id) +}