-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08_solution.jl
121 lines (101 loc) · 2.63 KB
/
day08_solution.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
f = open("day08_input.txt", "r")
matrix_str = split(read(f, String), "\r\n")
close(f)
#########################
# Part 1
#########################
matrix = hcat([[parse(Int, element) for element in row] for row in matrix_str]...)
nrows, ncols = size(matrix)
visible = zeros(Int, nrows, ncols)
# Loop over each row to find trees visible from left or right
for i in 1:nrows
# From left
curr_max = -1
for j in 1:ncols
if matrix[i, j] > curr_max
curr_max = matrix[i, j]
visible[i, j] = 1
end
end
# From right
curr_max = -1
for j in ncols:-1:1
if matrix[i, j] > curr_max
curr_max = matrix[i, j]
visible[i, j] = 1
end
end
end
# Loop over each column to find trees visible from top or bottom
for j in 1:ncols
# From top
curr_max = -1
for i in 1:nrows
if matrix[i, j] > curr_max
curr_max = matrix[i, j]
visible[i, j] = 1
end
end
# From right
curr_max = -1
for i in nrows:-1:1
if matrix[i, j] > curr_max
curr_max = matrix[i, j]
visible[i, j] = 1
end
end
end
# 1827
sum(visible)
#########################
# Part 2
#########################
scenic_score = zeros(Int, nrows, ncols)
for i in 2:(nrows-1)
for j in 2:(ncols-1)
# Look up
num_vis_up = 0
for i_view in (i-1):-1:1
if matrix[i_view, j] < matrix[i, j]
num_vis_up += 1
elseif matrix[i_view, j] >= matrix[i, j]
num_vis_up += 1
break
end
end
# Look down
num_vis_down = 0
for i_view in (i+1):nrows
if matrix[i_view, j] < matrix[i, j]
num_vis_down += 1
elseif matrix[i_view, j] >= matrix[i, j]
num_vis_down += 1
break
end
end
# Look left
num_vis_left = 0
for j_view in (j-1):-1:1
if matrix[i, j_view] < matrix[i, j]
num_vis_left += 1
elseif matrix[i, j_view] >= matrix[i, j]
num_vis_left += 1
break
end
end
# Look right
num_vis_right = 0
for j_view in (j+1):ncols
if matrix[i, j_view] < matrix[i, j]
num_vis_right += 1
elseif matrix[i, j_view] >= matrix[i, j]
num_vis_right += 1
break
end
end
scenic_score[i, j] = num_vis_left * num_vis_right * num_vis_up * num_vis_down
end
end
scenic_score
# 335580
maximum(scenic_score)