Advertisement
A2point0

Untitled

Dec 2nd, 2022
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program aoc22day2
  2.  
  3.     implicit none
  4.     integer :: ios
  5.     integer, parameter :: xmasunit = 99
  6.     integer, allocatable :: calories(:)
  7.     integer :: n, i, meal, m
  8.     character(len=999) :: line
  9.  
  10.     open(unit=xmasunit, file='input.txt', iostat=ios)
  11.     if ( ios /= 0 ) stop "Christmas is over"
  12.  
  13.     n = 0
  14.  
  15.     do
  16.         read(xmasunit, '(A)', iostat=ios) line
  17.         if (ios /= 0) exit
  18.         if (len_trim(line) == 0 ) n = n + 1
  19.     end do
  20.  
  21.     print*, "There exists:", n, "elves"
  22.  
  23.     allocate(calories(n+1))
  24.     rewind(xmasunit)
  25.     meal = 0
  26.     i = 0
  27.  
  28.     do
  29.         read(xmasunit, '(A)', iostat=ios) line
  30.         if (ios /= 0) exit  
  31.         if (len_trim(line) == 0 ) then
  32.             i = i + 1
  33.             calories(i) = meal
  34.             meal = 0
  35.         else
  36.             read (line,'(I10)') m
  37.             meal = meal + m
  38.         endif
  39.     end do
  40.     calories(n+1) = meal
  41.    
  42.     print*, "The highest amount of calories carried by an elf is: ", maxval(calories)
  43.  
  44.     close(xmasunit)
  45.  
  46. end program aoc22day2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement