-
Notifications
You must be signed in to change notification settings - Fork 4
/
day25.jl
36 lines (31 loc) · 1004 Bytes
/
day25.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
module Day25
using AdventOfCode2021
function day25(input::String = readInput(joinpath(@__DIR__, "..", "data", "day25.txt")))
watermap = map(x -> x[1], reduce(vcat, permutedims.(split.(split(input), ""))))
nsteps = 0
changed = true
while changed
changed = step!(watermap)
nsteps += 1
end
return nsteps
end
function step!(watermap::Matrix{Char})
changed = false
move_right = findall(==('>'), watermap)
filter!(x -> watermap[x[1], mod1(x[2] + 1, size(watermap, 2))] == '.', move_right)
for ci ∈ move_right
changed = true
watermap[ci] = '.'
watermap[ci[1], mod1(ci[2] + 1, size(watermap, 2))] = '>'
end
move_down = findall(==('v'), watermap)
filter!(x -> watermap[mod1(x[1] + 1, size(watermap, 1)), x[2]] == '.', move_down)
for ci ∈ move_down
changed = true
watermap[ci] = '.'
watermap[mod1(ci[1] + 1, size(watermap, 1)), ci[2]] = 'v'
end
return changed
end
end # module