diff options
author | J.J <thechairman@thechairman.info> | 2023-12-23 15:29:02 -0500 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2023-12-23 15:29:02 -0500 |
commit | ffaba850705af5a880038b611d9586573c2c5248 (patch) | |
tree | cfc2bbe8b77920f799c02d4a418bc6727fc1a630 /aoc2023/src/day23 | |
parent | eb492eea3adbba1995d870c6c4a9c22d09a4c96f (diff) | |
download | gleam_aoc-ffaba850705af5a880038b611d9586573c2c5248.tar.gz gleam_aoc-ffaba850705af5a880038b611d9586573c2c5248.zip |
day 23 gleam cleanup
Diffstat (limited to 'aoc2023/src/day23')
-rw-r--r-- | aoc2023/src/day23/solve.gleam | 48 |
1 files 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) { |