diff --git a/10.jl b/10.jl new file mode 100644 index 0000000..9824072 --- /dev/null +++ b/10.jl @@ -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 +