My yearly advent-of-code solutions
at main 1.2 kB view raw
1program day_04 2 implicit none 3 character(len=140) :: lines(140) 4 character(len=1) :: c 5 integer :: io, row, col, score, z 6 logical :: left_m, right_m 7 8 left_m = .false. 9 right_m = .false. 10 score = 0 11 open(newunit=io, file='./day_04_input.txt', status='old', action='read') 12 read(io, '(a)') lines 13 do row = 2, size(lines) - 1 14 do col = 2, len(lines(row)) - 1 15 if (lines(row)(col:col) == 'A') then 16 c = lines(row-1)(col-1:col-1) 17 if (c == 'S' .or. c == 'M') then 18 left_m = c == 'M' 19 c = lines(row-1)(col+1:col+1) 20 if (c == 'S' .or. c == 'M') then 21 right_m = c == 'M' 22 c = lines(row+1)(col-1:col-1) 23 if (right_m .and. c == 'S' .or. (.not. right_m .and. c == 'M')) then 24 c = lines(row+1)(col+1:col+1) 25 if (left_m .and. c == 'S' .or. (.not. left_m .and. c =='M')) then 26 score = score + 1 27 end if 28 end if 29 end if 30 end if 31 end if 32 left_m = .false. 33 right_m = .false. 34 c = '' 35 end do 36 end do 37 print*, score 38 39end program day_04