aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-03-08 12:13:34 -0500
committerH.J <thechairman@thechairman.info>2024-03-08 12:13:34 -0500
commit0e5611f2ef28ff0725cdd726d4c0a10c2ca9fa3a (patch)
tree94d7087b0cf3b236b0b9a47933ce08a48700ffd7
parent2a5e033b2d8882f50f2bfe4da25906c57f2a41f0 (diff)
downloadgleam_aoc-0e5611f2ef28ff0725cdd726d4c0a10c2ca9fa3a.tar.gz
gleam_aoc-0e5611f2ef28ff0725cdd726d4c0a10c2ca9fa3a.zip
Codingquest day 5 in Gleam
-rw-r--r--codingquest2024/src/day5/data.txt37
-rw-r--r--codingquest2024/src/day5/solution.gleam67
2 files changed, 104 insertions, 0 deletions
diff --git a/codingquest2024/src/day5/data.txt b/codingquest2024/src/day5/data.txt
new file mode 100644
index 0000000..9a954b1
--- /dev/null
+++ b/codingquest2024/src/day5/data.txt
@@ -0,0 +1,37 @@
+ base cg96 hx27 ik04 qx43 dc94 ap98 ux20 li84 rt50 el82 rd48 sf80 mg52 rc82 eu85 hr27 bi41 sh80 af18 vp88 km46 em44 pd69 ts86
+base 0 30638 48992 35639 74968 48045 6341 90127 47393 71441 19111 80113 80629 56954 81776 30862 30878 21854 77873 34270 86306 42311 15532 71522 79586
+cg96 30638 0 72821 29603 89651 17412 36569 100420 36168 79354 25142 62592 66534 41290 63249 57094 48704 8784 66650 8582 86685 42076 27940 52718 85170
+hx27 48992 72821 0 54515 34839 88023 43405 53755 62684 44187 48043 90438 85542 72549 93258 18509 25262 65340 79206 79456 66739 47748 44881 86136 53441
+ik04 35639 29603 54515 0 62700 38191 37769 71535 11907 50269 16570 44931 44989 22141 46865 44622 29623 28109 42498 38186 57377 12646 20780 37109 55645
+qx43 74968 89651 34839 62700 0 100846 70926 19095 64882 19976 65515 81427 73635 71560 84552 49712 44223 84464 66126 97774 40896 50837 64609 81039 25903
+dc94 48045 17412 88023 38191 100846 0 53925 109266 39656 87944 40040 57202 63371 40295 57004 73374 63104 26191 65469 17136 91082 50527 43684 47297 92341
+ap98 6341 36569 43405 37769 70926 53925 0 86798 49674 68892 21378 82673 82483 59670 84517 25041 27490 27800 79193 40546 85080 42672 16989 74486 77382
+ux20 90127 100420 53755 71535 19095 109266 86798 0 70601 21322 77982 79293 70326 74908 82378 67504 59364 96475 62788 108879 31128 58919 78000 81261 19223
+li84 47393 36168 62684 11907 64882 39656 49674 70601 0 49450 28416 33031 33361 10569 35013 55008 38971 37093 31581 44413 51620 15309 32686 25491 52956
+rt50 71441 79354 44187 50269 19976 87944 68892 21322 49450 0 57683 61913 53835 54424 65044 53222 41615 75794 46283 87860 23000 37634 58207 62319 9390
+el82 19111 25142 48043 16570 65515 40040 21378 77982 28416 57683 0 61342 61543 38297 63150 33772 23565 18965 58787 32642 69545 24118 4711 53111 64814
+rd48 80113 62592 90438 44931 81427 57202 82673 79293 33031 61913 61342 0 9678 23176 3130 86267 69538 66544 16684 69212 50346 44242 65695 10005 60148
+sf80 80629 66534 85542 44989 73635 63371 82483 70326 33361 53835 61543 9678 0 25343 12471 83231 66388 69377 7597 73797 40862 41575 65571 16923 51317
+mg52 56954 41290 72549 22141 71560 40295 59670 74908 10569 54424 38297 23176 25343 0 24857 65525 49341 44089 25635 48803 52073 24802 42743 14978 56289
+rc82 81776 63249 93258 46865 84552 57004 84517 82378 35013 65044 63150 3130 12471 24857 0 88758 72080 67560 19692 69606 53292 46776 67570 10597 63246
+eu85 30862 57094 18509 44622 49712 73374 25041 67504 55008 53222 33772 86267 83231 65525 88758 0 16843 48998 78001 62939 73562 42033 29734 80190 62528
+hr27 30878 48704 25262 29623 44223 63104 27490 59364 38971 41615 23565 69538 66388 49341 72080 16843 0 42021 61213 56009 59502 25305 21319 63773 50379
+bi41 21854 8784 65340 28109 84464 26191 27800 96475 37093 75794 18965 66544 69377 44089 67560 48998 42021 0 68546 14120 85334 39674 20747 56964 82328
+sh80 77873 66650 79206 42498 66126 65469 79193 62788 31581 46283 58787 16684 7597 25635 19692 78001 61213 68546 0 74364 33695 37154 62514 21598 43742
+af18 34270 8582 79456 38186 97774 17136 40546 108879 44413 87860 32642 69212 73797 48803 69606 62939 56009 14120 74364 0 95209 50649 34816 59231 93742
+vp88 86306 86685 66739 57377 40896 91082 85080 31128 51620 23000 69545 50346 40862 52073 53292 73562 59502 85334 33695 95209 0 45829 71442 54312 15000
+km46 42311 42076 47748 12646 50837 50527 42672 58919 15309 37634 24118 44242 41575 24802 46776 42033 25305 39674 37154 50649 45829 0 26815 38684 43094
+em44 15532 27940 44881 20780 64609 43684 16989 78000 32686 58207 4711 65695 65571 42743 67570 29734 21319 20747 62514 34816 71442 26815 0 57619 65780
+pd69 71522 52718 86136 37109 81039 47297 74486 81261 25491 62319 53111 10005 16923 14978 10597 80190 63773 56964 21598 59231 54312 38684 57619 0 62060
+ts86 79586 85170 53441 55645 25903 92341 77382 19223 52956 9390 64814 60148 51317 56289 63246 62528 50379 82328 43742 93742 15000 43094 65780 62060 0
+
+Rover 1 route: base -> km46 -> rd48 -> ts86 -> km46 -> base -> dc94 -> sh80 -> ux20 -> cg96 -> hr27 -> bi41 -> ap98 -> ux20 -> ik04 -> sf80 -> ik04 -> ik04 -> rd48 -> base
+Rover 2 route: base -> em44 -> km46 -> pd69 -> rt50 -> vp88 -> sf80 -> vp88 -> base -> cg96 -> rc82 -> mg52 -> bi41 -> hr27 -> base
+Rover 3 route: base -> base -> li84 -> km46 -> ts86 -> base -> sh80 -> pd69 -> em44 -> sh80 -> af18 -> base
+Rover 4 route: base -> ts86 -> ts86 -> af18 -> base -> ap98 -> rc82 -> bi41 -> bi41 -> qx43 -> em44 -> em44 -> el82 -> base
+Rover 5 route: base -> dc94 -> hx27 -> rt50 -> vp88 -> pd69 -> af18 -> sf80 -> base
+Rover 6 route: base -> hx27 -> eu85 -> em44 -> dc94 -> ux20 -> vp88 -> ik04 -> ux20 -> li84 -> mg52 -> base
+Rover 7 route: base -> ap98 -> ts86 -> ts86 -> em44 -> rt50 -> rt50 -> qx43 -> sh80 -> ap98 -> ux20 -> el82 -> rt50 -> km46 -> base
+Rover 8 route: base -> pd69 -> ap98 -> ik04 -> el82 -> sf80 -> ux20 -> vp88 -> dc94 -> pd69 -> ik04 -> li84 -> ts86 -> base
+Rover 9 route: base -> sh80 -> km46 -> ts86 -> base -> sf80 -> eu85 -> ap98 -> sh80 -> qx43 -> rt50 -> bi41 -> ux20 -> bi41 -> dc94 -> pd69 -> vp88 -> li84 -> ik04 -> base
+Rover 10 route: base -> ts86 -> sh80 -> em44 -> em44 -> af18 -> base -> eu85 -> ik04 -> af18 -> sh80 -> sf80 -> dc94 -> ap98 -> cg96 -> dc94 -> qx43 -> sh80 -> rd48 -> rc82 -> base \ No newline at end of file
diff --git a/codingquest2024/src/day5/solution.gleam b/codingquest2024/src/day5/solution.gleam
new file mode 100644
index 0000000..6c10693
--- /dev/null
+++ b/codingquest2024/src/day5/solution.gleam
@@ -0,0 +1,67 @@
+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) })
+}