AdventOfFortran/2023/src/day01.f90

54 lines
1.4 KiB
Fortran

program day1b
implicit none
character(300) :: data_line
character(1) :: test_char
integer :: num1, num2
integer :: numsum
integer :: running_total = 0
integer :: i, istat
integer :: i2, strlen
integer, parameter :: max_iterations = 10000
integer :: n_arguments
character(200) :: fname
n_arguments = command_argument_count()
if (n_arguments .eq. 1) then
call get_command_argument(1, fname)
print *, "File: ", fname
else
print *, "Wrong number of arguments: ", n_arguments
stop
end if
open(10, file=fname)
do i=1,10000
read(10, "(A)", iostat = istat) data_line
if (is_iostat_end(istat)) then
exit
end if
num1 = -1
num2 = -1
strlen = len_trim(data_line)
do i2=1,strlen
test_char = data_line(i2:i2)
if ((test_char .ge. '0') .and. (test_char .le. '9')) then
read(test_char, *) num2
if (num1 .eq. -1) then
num1 = num2
end if
end if
end do
if (num1 .ne. -1) then
numsum = num1 * 10 + num2
running_total = running_total + numsum
write(*, 20) num1, num2, numsum, data_line(1:strlen)
end if
end do
close(10)
print *
print *, "Sum of calibration values: ", running_total
20 format(i1, i2, i4, '| ', a)
end program day1b