changeset 19:9b6e6ace0e72

Day 12 part 2
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 14 Dec 2022 21:16:38 +0100
parents 6ad216d8abee
children 6160397412c3
files 12/12.jl
diffstat 1 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/12/12.jl	Wed Dec 14 21:12:55 2022 +0100
+++ b/12/12.jl	Wed Dec 14 21:16:38 2022 +0100
@@ -40,8 +40,12 @@
     v
 end
 
-function find_path(m::Matrix{Int})
+function find_path(m::Matrix{Int}; start::Union{Nothing, CartesianIndex}=nothing)::Vector{CartesianIndex}
     s, e = find_start_end(m);
+    if !isnothing(start)
+        s = start;
+    end
+
     current_best = zeros(Int, size(m)) .+ 10000;
     parents = [CartesianIndex((-1,-1)) for i = 1:size(m, 1), j = 1:size(m, 2)];
     top = PriorityQueue{Tuple{CartesianIndex,Int}, Int}();
@@ -69,7 +73,14 @@
             end
         end
     end
-    error("no path found!");
+    []
+end
+
+function find_any_shortest_path(m::Matrix{Int})::Vector{Int}
+    positions = findall(x -> x == START || x == 0, m);
+    lengths = [length(find_path(m; start=p)) for p in positions];
+    sort!(lengths);
+    lengths
 end
 
 open(input; read=true) do fh
@@ -79,4 +90,6 @@
     end
     @time p = find_path(m);
     println("PART 1: Path $p with length $(length(p))");
+
+    println("PART 2: All path lengths are $(find_any_shortest_path(m))");
 end