commit 3afc67b9cb99a79240f14d4247eed371dbf47025
parent 2d2c3b50224a67ec8b52aa871246c7672fd7c088
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Thu, 15 Dec 2022 20:46:45 -0800
Solution to day 12, part 1.
Finally. I misunderstood the constraints, lol.
Diffstat:
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/src/day_12.jl b/src/day_12.jl
@@ -32,17 +32,25 @@ end
function makegraph(heightmap::AbstractMatrix)
height, width = size(heightmap)
- g = Graph(height*width)
+ g = DiGraph(height*width)
# I wanted to do this elegantly, but meh, just going to brute-force
# indexing.
for (i, ind) = enumerate(CartesianIndices(heightmap))
- if ind[1] < height &&
- abs(heightmap[ind] - heightmap[ind[1]+1, ind[2]]) <= 1
- add_edge!(g, i, i+1)
+ if ind[1] < height
+ if heightmap[ind] + 1 >= heightmap[ind[1]+1, ind[2]]
+ add_edge!(g, i, i+1)
+ end
+ if heightmap[ind[1]+1, ind[2]] + 1 >= heightmap[ind]
+ add_edge!(g, i+1, i)
+ end
end
- if ind[2] < width &&
- abs(heightmap[ind] - heightmap[ind[1], ind[2]+1]) <= 1
- add_edge!(g, i, i+height)
+ if ind[2] < width
+ if heightmap[ind] + 1 >= heightmap[ind[1], ind[2]+1]
+ add_edge!(g, i, i+height)
+ end
+ if heightmap[ind[1], ind[2]+1] + 1 >= heightmap[ind]
+ add_edge!(g, i+height, i)
+ end
end
end
g