From ffaba850705af5a880038b611d9586573c2c5248 Mon Sep 17 00:00:00 2001 From: "J.J" Date: Sat, 23 Dec 2023 15:29:02 -0500 Subject: day 23 gleam cleanup --- aoc2023/src/day23/solve.gleam | 48 ++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/aoc2023/src/day23/solve.gleam b/aoc2023/src/day23/solve.gleam index 916ef17..e1fe638 100644 --- a/aoc2023/src/day23/solve.gleam +++ b/aoc2023/src/day23/solve.gleam @@ -84,45 +84,37 @@ fn walk_to_next_junction( } } +fn find_routes(junctions, trails) { + use junction <- list.flat_map(junctions) + use neighbor <- list.filter_map(junction_neighbors(junction)) + case dict.has_key(trails, neighbor) { + True -> Ok(start_walking_to_next_junction(junction, neighbor, trails)) + False -> Error(Nil) + } +} + fn generate_routes( junctions: List(Posn), trails: Array2D(Path), ) -> Dict(Posn, List(Route)) { - { - use junction <- list.flat_map(junctions) - use neighbor <- list.filter_map(junction_neighbors(junction)) - case dict.has_key(trails, neighbor) { - True -> Ok(start_walking_to_next_junction(junction, neighbor, trails)) - False -> Error(Nil) - } - } - |> list.fold(dict.new(), fn(acc, edge) { - let #(from, route) = edge - use v <- dict.update(acc, from) - case v { - None -> [route] - Some(routes) -> [route, ..routes] - } - }) + use acc, #(from, route) <- list.fold( + find_routes(junctions, trails), + dict.new(), + ) + dict.update(acc, from, append_to_key(_, route)) } fn generate_2way_routes( junctions: List(Posn), trails: Array2D(Path), ) -> Dict(Posn, List(Route)) { - let routes = { - use junction <- list.flat_map(junctions) - use neighbor <- list.filter_map(junction_neighbors(junction)) - case dict.has_key(trails, neighbor) { - True -> Ok(start_walking_to_next_junction(junction, neighbor, trails)) - False -> Error(Nil) - } - } - use acc, r <- list.fold(routes, dict.new()) - let #(from, Route(to, dist)) = r + use acc, #(from, route) <- list.fold( + find_routes(junctions, trails), + dict.new(), + ) acc - |> dict.update(from, append_to_key(_, Route(to, dist))) - |> dict.update(to, append_to_key(_, Route(from, dist))) + |> dict.update(from, append_to_key(_, route)) + |> dict.update(route.to, append_to_key(_, Route(from, route.distance))) } fn dfs(routes, from, to) { -- cgit v1.2.3