problem_1 Program

Uses

  • program~~problem_1~~UsesGraph program~problem_1 problem_1 iso_fortran_env iso_fortran_env program~problem_1->iso_fortran_env module~aoc_utilities aoc_utilities program~problem_1->module~aoc_utilities module~aoc_utilities->iso_fortran_env

Calls

program~~problem_1~~CallsGraph program~problem_1 problem_1 proc~clock_end aoc_utilities::clock%clock_end program~problem_1->proc~clock_end proc~clock_start aoc_utilities::clock%clock_start program~problem_1->proc~clock_start proc~number_of_lines_in_file aoc_utilities::number_of_lines_in_file program~problem_1->proc~number_of_lines_in_file proc~read_line aoc_utilities::read_line program~problem_1->proc~read_line

Variables

Type Attributes Name Initial
integer :: iunit
logical :: status_ok
character(len=:), allocatable :: line
integer :: n_lines
integer :: i
integer :: j
integer :: k
integer :: n
integer :: isum
integer :: ifirst
integer :: ilast
integer :: idxfirst
integer :: idxlast
character(len=*), parameter, dimension(9) :: ichars = ['one  ', 'two  ', 'three', 'four ', 'five ', 'six  ', 'seven', 'eight', 'nine ']

Source Code

program problem_1

use iso_fortran_env
use aoc_utilities

implicit none

integer :: iunit
logical :: status_ok
character(len=:),allocatable :: line
integer :: n_lines
integer :: i, j, k, n, isum, ifirst, ilast, idxfirst, idxlast

character(len=*),dimension(9),parameter :: ichars = [ &
    'one  ', &
    'two  ', &
    'three', &
    'four ', &
    'five ', &
    'six  ', &
    'seven', &
    'eight', &
    'nine ']

call clk%tic()

isum = 0
open(newunit=iunit, file='inputs/day1.txt', status='OLD')
n_lines = number_of_lines_in_file(iunit)
do i = 1, n_lines

    ! remove all the chars before the first and after the last number
    line = read_line(iunit,status_ok)
    n = len(line)
    do j = 1, n
        if (line(j:j)>='1' .and. line(j:j)<='9') exit ! done
        line(j:j) = ' '
    end do

    do j = n, 1, -1
        if (line(j:j)>='1' .and. line(j:j)<='9') exit ! done
        line(j:j) = ' '
    end do

    line = trim(adjustl(line))
    n = len(line)
    isum  = isum + 10*int(line(1:1)) + int(line(n:n))

end do
write(*,*) '1a: sum:', isum
close(iunit)

! -------- part 2 -----------

open(newunit=iunit, file='inputs/day1.txt', status='OLD')
n_lines = number_of_lines_in_file(iunit)
isum = 0
do i = 1, n_lines

    line = read_line(iunit,status_ok)
    n = len(line)

    ! keep track of the index of the first and last number (digit or string)
    ifirst = huge(1)
    idxfirst = huge(1)
    do j = 1, size(ichars)
        k = min( index(line,trim(ichars(j))), index(line,trim(ichars(j)),back=.true.) )  ! accounts for duplicates
        if (k>0 .and. k<=idxfirst) then
            ifirst = j
            idxfirst = k
        end if
    end do
    do j = 1, n
        if (line(j:j)>='1' .and. line(j:j)<='9') then
            if (j <= idxfirst) then
                ifirst = int(line(j:j))
                idxfirst = j
            end if
        end if
    end do

    ilast = 0
    idxlast = 0
    do j = 1, size(ichars)
        k = max( index(line,trim(ichars(j))), index(line,trim(ichars(j)), back=.true.) )  ! accounts for duplicates
        if (k>0 .and. k>=idxlast) then
            ilast = j
            idxlast = k
        end if
    end do
    do j = 1, n
        if (line(j:j)>='1' .and. line(j:j)<='9') then
            if (j >= idxlast) then
                ilast = int(line(j:j))
                idxlast = j
            end if
        end if
    end do

    isum  = isum + 10*ifirst + ilast

end do
write(*,*) '1b: sum:', isum

call clk%toc('1')

end program problem_1