Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program aoc22day2
- implicit none
- integer :: ios
- integer, parameter :: xmasunit = 99
- integer, allocatable :: calories(:)
- integer :: n, i, meal, m
- character(len=999) :: line
- open(unit=xmasunit, file='input.txt', iostat=ios)
- if ( ios /= 0 ) stop "Christmas is over"
- n = 0
- do
- read(xmasunit, '(A)', iostat=ios) line
- if (ios /= 0) exit
- if (len_trim(line) == 0 ) n = n + 1
- end do
- print*, "There exists:", n, "elves"
- allocate(calories(n+1))
- rewind(xmasunit)
- meal = 0
- i = 0
- do
- read(xmasunit, '(A)', iostat=ios) line
- if (ios /= 0) exit
- if (len_trim(line) == 0 ) then
- i = i + 1
- calories(i) = meal
- meal = 0
- else
- read (line,'(I10)') m
- meal = meal + m
- endif
- end do
- calories(n+1) = meal
- print*, "The highest amount of calories carried by an elf is: ", maxval(calories)
- close(xmasunit)
- end program aoc22day2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement