-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.f90
170 lines (144 loc) · 4.65 KB
/
day08.f90
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module day08
implicit none
contains
function read_file( filename, grid_size ) result(grid)
character(len=*), intent(in) :: filename
integer, intent(in) :: grid_size
integer :: i, j, n
integer :: grid(grid_size, grid_size)
character(len=grid_size) :: line
open( unit=1, file=filename, status='old' )
do i = 1, grid_size
read( 1, '(a)', iostat=n ) line
if ( n /= 0 ) exit
do j = 1, grid_size
read(line(j:j), *) grid(i,j)
end do
end do
close( 1 )
grid = grid
end function read_file
function calc_visibility( grid, grid_size ) result(visibility)
integer, intent(in) :: grid(:,:)
integer, intent(in) :: grid_size
logical :: visibility(grid_size, grid_size)
integer :: row, col, x
do row = 1, grid_size
do col = 1, grid_size
visibility(row, col) = .true.
end do
end do
do row = 2, grid_size - 1
loop: do col = 2, grid_size - 1
visibility(row, col) = .false.
! go to the left
do x = col - 1, 1, -1
if ( grid(row, x) >= grid(row, col) ) then
go to 10
end if
end do
visibility(row, col) = .true.
cycle loop
! go to the right
10 do x = col + 1, grid_size, 1
if ( grid(row, x) >= grid(row, col) ) then
go to 20
end if
end do
visibility(row, col) = .true.
cycle loop
! go up
20 do x = row - 1, 1, -1
if ( grid(x, col) >= grid(row, col) ) then
go to 30
end if
end do
visibility(row, col) = .true.
cycle loop
! go down
30 do x = row + 1, grid_size, 1
if ( grid(x, col) >= grid(row, col) ) then
cycle loop
end if
end do
visibility(row, col) = .true.
end do loop
end do
visibility = visibility
end function calc_visibility
function count_visible_trees( visibility ) result(number)
logical, intent(in) :: visibility(:,:)
integer :: number
number = count( visibility )
end function count_visible_trees
function find_max_scenic_score( grid, grid_size ) result(max_score)
integer, intent(in) :: grid(:,:)
integer, intent(in) :: grid_size
integer :: row, col, x, score, max_score
integer :: dist_left, dist_right, dist_up, dist_down
max_score = 0
do row = 2, grid_size - 1
do col = 2, grid_size - 1
dist_left = 0
dist_right = 0
dist_up = 0
dist_down = 0
score = 0
! go to the left
do x = col - 1, 1, -1
dist_left = dist_left + 1
if ( grid(row, x) >= grid(row, col) ) then
go to 10
end if
end do
! go to the right
10 do x = col + 1, grid_size, 1
dist_right = dist_right + 1
if ( grid(row, x) >= grid(row, col) ) then
go to 20
end if
end do
! go up
20 do x = row - 1, 1, -1
dist_up = dist_up + 1
if ( grid(x, col) >= grid(row, col) ) then
go to 30
end if
end do
! go down
30 do x = row + 1, grid_size, 1
dist_down = dist_down + 1
if ( grid(x, col) >= grid(row, col) ) then
go to 40
end if
end do
40 score = dist_left * dist_right * dist_up * dist_down
if ( score > max_score ) then
max_score = score
end if
end do
end do
end function find_max_scenic_score
subroutine print_grid( grid )
integer, intent(in) :: grid(:,:)
integer :: row, col
do row = 1, size(grid, 2)
write(*, fmt="(1x,a)", advance="no") " "
do col = 1, size(grid, 1)
write(*, fmt="(i0)", advance="no") grid(row, col)
end do
print *, ''
end do
end subroutine print_grid
subroutine print_visibility( visibility )
logical, intent(in) :: visibility(:,:)
integer :: row, col
do row = 1, size(visibility, 2)
write(*, fmt="(1x,a)", advance="no") " "
do col = 1, size(visibility, 1)
write(*, fmt="(L)", advance="no") visibility(row, col)
end do
print *, ''
end do
end subroutine print_visibility
end module day08