Day 10
This commit is contained in:
parent
be200366b9
commit
b94a318fc3
1 changed files with 34 additions and 0 deletions
34
10.jl
Normal file
34
10.jl
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue