34 lines
736 B
Julia
34 lines
736 B
Julia
const grid::Vector{Vector{UInt8}} = [ [ UInt8(x) - UInt8('0') for x in collect(line) ] for line in readlines("input/10") ]
|
|
const size = length(grid)
|
|
|
|
isOnMap(x, y) = 1 <= x <= size && 1 <= y <= size
|
|
|
|
function checkTrail(x, y, reached)
|
|
local c = grid[y][x]
|
|
if c == 9
|
|
push!(reached, (x,y))
|
|
return 1
|
|
end
|
|
local score = 0
|
|
for (u,v) in ((x+1, y), (x-1, y),(x, y+1), (x, y-1))
|
|
if isOnMap(u, v) && grid[v][u] == c+1
|
|
score += checkTrail(u, v, reached)
|
|
end
|
|
end
|
|
return score
|
|
end
|
|
|
|
partOne, partTwo = 0, 0
|
|
|
|
for y in 1:size
|
|
for x in 1:size
|
|
if grid[y][x] == 0
|
|
local reached::Set{Tuple{Int,Int}} = Set()
|
|
global partTwo += checkTrail(x, y, reached)
|
|
global partOne += length(reached)
|
|
end
|
|
end
|
|
end
|
|
|
|
@show partOne, partTwo
|
|
|