-
Notifications
You must be signed in to change notification settings - Fork 7
/
9.jl
57 lines (51 loc) · 1.36 KB
/
9.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
57
lines = (collect).(readlines())
function is_valid(y, x)
return 1 <= x <= length(lines[1]) && 1 <= y <= length(lines)
end
function solve_a()
s = 0
for y in 1:length(lines)
for x in 1:length(lines[y])
good = true
for (xa, ya) in ((-1, 0), (1, 0), (0, 1), (0, -1))
if is_valid(y + ya, x + xa)
if lines[y + ya][x + xa] <= lines[y][x]
good = false
end
end
end
if good
s += parse(Int, lines[y][x]) + 1
end
end
end
return s
end
function solve_b()
basins = []
for Y in 1:length(lines)
for X in 1:length(lines[Y])
queue = [(Y, X)]
s = 0
while !isempty(queue)
y, x = popfirst!(queue)
if lines[y][x] != '9'
lines[y][x] = '9'
s += 1
for (xa, ya) in ((-1, 0), (1, 0), (0, 1), (0, -1))
if is_valid(y + ya, x + xa)
push!(queue, (y + ya, x + xa))
end
end
end
end
push!(basins, s)
end
end
return prod(sort(basins)[end-2:end])
end
function main()
println(solve_a())
println(solve_b())
end
main()