about summary refs log tree commit diff
path: root/aoc/2022/08/part-two.f90
blob: a313638149bc6be1b2c2c0ba71d3b7b3c4808145 (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
48
49
50
51
52
53
PROGRAM two
  implicit none
  integer, parameter :: WIDTH = 99, HEIGHT = 99
  integer, parameter :: ID(0:9) = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  integer, parameter :: COL(HEIGHT, 0:9) = spread(ID, 1, HEIGHT)
  integer, parameter :: ROW(WIDTH, 0:9) = spread(ID, 1, WIDTH)
  character(WIDTH) :: lines(HEIGHT)
  integer :: i, j
  integer, dimension(HEIGHT, WIDTH) :: grid, score
  integer, dimension(HEIGHT, WIDTH, 0:9) :: tmp

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

  score = 0
  tmp = 0
  do i = 2, HEIGHT-1
    tmp(i, :, :) = merge(tmp(i-1, :, :)+1, 1, spread(grid(i-1, :), 2, 10) < ROW)
    do j = 2, WIDTH-1
      score(i, j) = tmp(i, j, grid(i, j))
    end do
  end do

  tmp = 0
  do i = 2, WIDTH-1
    tmp(:, i, :) = merge(tmp(:, i-1, :)+1, 1, spread(grid(:, i-1), 2, 10) < COL)
    do j = 2, WIDTH-1
      score(j, i) = score(j, i) * tmp(j, i, grid(j, i))
    end do
  end do

  tmp = 0
  do i = HEIGHT-1, 2, -1
    tmp(i, :, :) = merge(tmp(i+1, :, :)+1, 1, spread(grid(i+1, :), 2, 10) < ROW)
    do j = 2, WIDTH-1
      score(i, j) = score(i, j) * tmp(i, j, grid(i, j))
    end do
  end do

  tmp = 0
  do i = WIDTH-1, 2, -1
    tmp(:, i, :) = merge(tmp(:, i+1, :)+1, 1, spread(grid(:, i+1), 2, 10) < COL)
    do j = 2, WIDTH-1
      score(j, i) = score(j, i) * tmp(j, i, grid(j, i))
    end do
  end do

  print *, maxval(score)
end program two