diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /codingquest2024/src/day5 | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'codingquest2024/src/day5')
-rw-r--r-- | codingquest2024/src/day5/solution.gleam | 67 |
1 files changed, 0 insertions, 67 deletions
diff --git a/codingquest2024/src/day5/solution.gleam b/codingquest2024/src/day5/solution.gleam deleted file mode 100644 index 6c10693..0000000 --- a/codingquest2024/src/day5/solution.gleam +++ /dev/null @@ -1,67 +0,0 @@ -import gleam/dict.{type Dict} -import gleam/io -import gleam/int -import gleam/list -import gleam/regex -import gleam/result -import gleam/string -import simplifile - -type Atlas = - Dict(String, Dict(String, Int)) - -pub fn main() { - let assert Ok(data) = simplifile.read(from: "./src/day5/data.txt") - - let assert [table, routes] = string.split(data, "\n\n") - let assert [header, ..rows] = string.split(table, "\n") - - let dests = - header - |> string.trim() - |> split_on_many_spaces - - let atlas = build_atlas(rows, dests) - - routes - |> string.split("\n") - |> list.fold(0, fn(acc, route) { acc + find_total_distance(route, atlas) }) - |> io.debug -} - -fn split_on_many_spaces(str) { - let assert Ok(re_spaces) = regex.from_string("\\s+") - regex.split(re_spaces, str) -} - -fn build_atlas(rows, dests) { - rows - |> list.map(split_on_many_spaces) - |> list.fold(dict.new(), fn(acc, row) { - let assert [from, ..raw_dists] = row - let assert Ok(dists) = list.try_map(raw_dists, int.parse) - let to_dict = - dests - |> list.zip(dists) - |> dict.from_list() - - dict.insert(acc, from, to_dict) - }) -} - -fn dist_between(leg: #(String, String), dict: Atlas) -> Int { - let assert Ok(dist) = - dict - |> dict.get(leg.0) - |> result.try(dict.get(_, leg.1)) - - dist -} - -fn find_total_distance(row: String, atlas: Atlas) { - let assert [_, route] = string.split(row, ": ") - - string.split(route, " -> ") - |> list.window_by_2 - |> list.fold(0, fn(acc, leg) { acc + dist_between(leg, atlas) }) -} |