aboutsummaryrefslogtreecommitdiff
path: root/2024/day_04_part_2.f90
blob: ed363ac145cb3a58e301b7135648b8e653d1f6df (plain)
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
program day_04
   implicit none
   character(len=140) ::  lines(140)
   character(len=1) :: c
   integer :: io, row, col, score, z
   logical :: left_m, right_m

   left_m = .false.
   right_m = .false.
   score = 0
   open(newunit=io, file='./day_04_input.txt', status='old', action='read')
   read(io, '(a)') lines
   do row = 2, size(lines) - 1
      do col = 2, len(lines(row)) - 1
         if (lines(row)(col:col) == 'A') then
            c = lines(row-1)(col-1:col-1)
            if (c == 'S' .or. c == 'M') then
               left_m = c == 'M'
               c = lines(row-1)(col+1:col+1)
               if (c == 'S' .or. c == 'M') then
                  right_m = c == 'M'
                  c = lines(row+1)(col-1:col-1)
                  if (right_m .and. c == 'S' .or. (.not. right_m .and. c == 'M')) then
                     c = lines(row+1)(col+1:col+1)
                     if (left_m .and. c == 'S' .or. (.not. left_m .and. c =='M')) then
                        score = score + 1
                     end if
                  end if
               end if
            end if
         end if
         left_m = .false.
         right_m = .false.
         c = ''
      end do
   end do
   print*, score

end program day_04