-
Notifications
You must be signed in to change notification settings - Fork 4
/
day09.jl
56 lines (50 loc) · 1.94 KB
/
day09.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Day09
using AdventOfCode2021
function day09(input::String = readInput(joinpath(@__DIR__, "..", "data", "day09.txt")))
heightmap = reduce(vcat, permutedims.(map(x -> parse.(Int, split(x, "")), split(input))))
p1, locations = part1(heightmap)
p2 = part2(heightmap, locations)
return [p1, p2]
end
function part1(heightmap::Matrix{Int})
risk = 0
locations = CartesianIndex{2}[]
m, n = size(heightmap)
for i ∈ 1:m
for j ∈ 1:n
lower = (
i + 1 > m || heightmap[i+1, j] > heightmap[i, j] ? true : false,
i - 1 < 1 || heightmap[i-1, j] > heightmap[i, j] ? true : false,
j + 1 > n || heightmap[i, j+1] > heightmap[i, j] ? true : false,
j - 1 < 1 || heightmap[i, j-1] > heightmap[i, j] ? true : false,
)
if all(lower)
risk += 1 + heightmap[i, j]
push!(locations, CartesianIndex(i, j))
end
end
end
return risk, locations
end
function part2(heightmap::Matrix{Int}, bassins::Vector{CartesianIndex{2}})
fillmap = zeros(Int, size(heightmap))
for (i, bas) in enumerate(bassins)
fill!(fillmap, [bas], i, heightmap)
end
return sort!([count(fillmap .== i) for i ∈ 1:length(bassins)], rev = true)[1:3] |> prod
end
function fill!(fillmap::Matrix{Int}, locations::Vector{CartesianIndex{2}}, fillvalue::Int, heightmap::Matrix{Int})
length(locations) == 0 && return
i, j = popfirst!(locations).I
fillmap[i, j] = fillvalue
for (k, l) in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1))
!(k >= 1 && k <= size(heightmap)[1] && l >= 1 && l <= size(heightmap)[2]) && continue
fillmap[k, l] == fillvalue && continue
heightmap[k, l] == 9 && continue
if heightmap[k, l] > heightmap[i, j]
push!(locations, CartesianIndex(k, l))
end
end
fill!(fillmap, locations, fillvalue, heightmap)
end
end # module