-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.jl
89 lines (76 loc) · 2.11 KB
/
08.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
include("utils.jl")
cookie = ""
input = get_aoc_input(8, cookie)
grid = permutedims(parse.(Int, hcat(collect.(split(strip(input)))...)))
"""
is_visible(trees)
Determine visible trees (from the left) in a one-dimensional array.
"""
function is_visible(trees)
visible = zeros(Bool, size(trees))
highest = -1
for i in eachindex(trees)
if trees[i] == maximum(trees)
visible[i] = true
return visible
end
if trees[i] > highest
highest = trees[i]
visible[i] = true
end
end
return visible
end
flipud(x) = reverse(x, dims=1)
fliplr(x) = reverse(x, dims=2)
function count_visible_trees(grid)
left = permutedims(hcat(is_visible.(collect(eachrow(grid)))...))
right = fliplr(permutedims(hcat(is_visible.(collect(eachrow(fliplr(grid))))...)))
top = hcat(is_visible.(collect(eachcol(grid)))...)
bottom = flipud(hcat(is_visible.(collect(eachcol(flipud(grid))))...))
return sum(left .| right .| top .| bottom)
end
# part 1
println("Visible trees: ", count_visible_trees(grid))
# part 2
function scenic_scores(grid)
scores = zero(grid)
for i in CartesianIndices(grid)
r, c = Tuple(i)
# look up
up = 0
if r > 1
for tree in grid[r-1:-1:1, c]
up += 1
tree >= grid[r, c] && break
end
end
# look down
down = 0
if r < size(grid, 1)
for tree in grid[r+1:end, c]
down += 1
tree >= grid[r, c] && break
end
end
# look right
right = 0
if c < size(grid, 2)
for tree in grid[r, c+1:end]
right += 1
tree >= grid[r, c] && break
end
end
# look left
left = 0
if c > 1
for tree in grid[r, c-1:-1:1]
left += 1
tree >= grid[r, c] && break
end
end
scores[r, c] = up * down * right * left
end
return scores
end
println("Highest scenic score: ", maximum(scenic_scores(grid)))