about summary refs log tree commit diff
path: root/aoc
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-09 17:03:50 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-09 18:28:07 +0900
commitf8b0528d933f4b7ac64c8460ed765e8e41240d7d (patch)
tree321e6ff18a9b96a5bdb264bb2f5c358afc2a43b0 /aoc
parentf82f0b1a08f1facebc6c334c6d881d97e403e269 (diff)
downloadcp-f8b0528d933f4b7ac64c8460ed765e8e41240d7d.tar.gz
[aoc/2022] Finish day 8
Diffstat (limited to 'aoc')
-rw-r--r--aoc/2022/08/part-one.f9047
-rw-r--r--aoc/2022/08/part-two.f9053
2 files changed, 100 insertions, 0 deletions
diff --git a/aoc/2022/08/part-one.f90 b/aoc/2022/08/part-one.f90
new file mode 100644
index 0000000..a339f48
--- /dev/null
+++ b/aoc/2022/08/part-one.f90
@@ -0,0 +1,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
diff --git a/aoc/2022/08/part-two.f90 b/aoc/2022/08/part-two.f90
new file mode 100644
index 0000000..a313638
--- /dev/null
+++ b/aoc/2022/08/part-two.f90
@@ -0,0 +1,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