about summary refs log tree commit diff
path: root/aoc/2022/08/part-one.f90
blob: a339f482e894b0734d65ae17a3f2217527ba2664 (plain) (blame)
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
PROGRAM one
  implicit none
  integer, parameter :: WIDTH = 99, HEIGHT = 99
  character(WIDTH), dimension(HEIGHT) :: lines
  integer :: i, j
  integer, dimension(HEIGHT, WIDTH) :: grid, tmp
  logical, dimension(HEIGHT, WIDTH) :: visible

  read (*, *) lines
  do i = 1, HEIGHT
    do j = 1, WIDTH
      read (lines(i)(j:j), '(I1)') grid(i, j)
    end do
  end do

  visible(:, :) = .false.
  visible(1, :) = .true.
  visible(HEIGHT, :) = .true.
  visible(:, 1) = .true.
  visible(:, WIDTH) = .true.

  tmp = grid
  do i = 2, HEIGHT-1
    visible(i, :) = visible(i, :) .or. tmp(i-1, :) < grid(i, :)
    tmp(i, :) = max(tmp(i-1, :), grid(i, :))
  end do

  tmp = grid
  do i = HEIGHT-1, 2, -1
    visible(i, :) = visible(i, :) .or. tmp(i+1, :) < grid(i, :)
    tmp(i, :) = max(tmp(i+1, :), grid(i, :))
  end do

  tmp = grid
  do i = 2, WIDTH-1
    visible(:, i) = visible(:, i) .or. tmp(:, i-1) < grid(:, i)
    tmp(:, i) = max(tmp(:, i-1), grid(:, i))
  end do

  tmp = grid
  do i = WIDTH-1, 2, -1
    visible(:, i) = visible(:, i) .or. tmp(:, i+1) < grid(:, i)
    tmp(:, i) = max(tmp(:, i+1), grid(:, i))
  end do

  print *, sum(merge(1, 0, visible))
end program one