problem_9 Program

Uses

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

Calls

program~~problem_9~~CallsGraph program~problem_9 problem_9 interface~parse aoc_utilities::parse program~problem_9->interface~parse proc~clock_end aoc_utilities::clock%clock_end program~problem_9->proc~clock_end proc~clock_start aoc_utilities::clock%clock_start program~problem_9->proc~clock_start proc~extrapolate problem_9::extrapolate program~problem_9->proc~extrapolate proc~number_of_lines_in_file aoc_utilities::number_of_lines_in_file program~problem_9->proc~number_of_lines_in_file proc~read_line aoc_utilities::read_line program~problem_9->proc~read_line proc~reverse aoc_utilities::reverse program~problem_9->proc~reverse proc~parse_nums64 aoc_utilities::parse_nums64 interface~parse->proc~parse_nums64 proc~diff~2 aoc_utilities::diff proc~extrapolate->proc~diff~2 interface~split aoc_utilities::split proc~parse_nums64->interface~split proc~split1 aoc_utilities::split1 interface~split->proc~split1 proc~split2 aoc_utilities::split2 interface~split->proc~split2 proc~expand_vector aoc_utilities::expand_vector proc~split1->proc~expand_vector proc~split2->proc~split1

Variables

Type Attributes Name Initial
integer :: i
integer :: iunit
integer :: n_lines
integer(kind=ip) :: isum

Functions

pure function extrapolate(ivals) result(iextrap)

extrapolate the next value in the sequence using a difference table. Straightfoward implemention: create the full table, and then evaluate it.

Arguments

Type IntentOptional Attributes Name
integer(kind=ip), intent(in), dimension(:) :: ivals

Return Value integer(kind=ip)


Source Code

program problem_9

use iso_fortran_env
use aoc_utilities

implicit none

integer :: i, iunit, n_lines
integer(ip) :: isum

call clk%tic()

! read the data file:
! open(newunit=iunit, file='inputs/day9_test.txt', status='OLD')
open(newunit=iunit, file='inputs/day9.txt', status='OLD')
n_lines = number_of_lines_in_file(iunit)
isum = sum([(extrapolate(parse(read_line(iunit))), i = 1, n_lines)])
close(iunit)
write(*,*) '9a: sum: ', isum

open(newunit=iunit, file='inputs/day9.txt', status='OLD')
n_lines = number_of_lines_in_file(iunit)
isum = sum([(extrapolate(reverse(parse(read_line(iunit)))), i = 1, n_lines)])
close(iunit)
write(*,*) '9b: sum: ', isum

call clk%toc('9')

contains

    pure function extrapolate(ivals) result(iextrap)
    !! extrapolate the next value in the sequence using
    !! a difference table. Straightfoward implemention:
    !! create the full table, and then evaluate it.
    integer(ip),dimension(:),intent(in) :: ivals
    integer(ip) :: iextrap
    integer :: i, n
    type(int64_vec),dimension(:),allocatable :: diff_table !! difference table (vector of vectors)

    ! create the difference table:
    diff_table = [int64_vec(ivals)]; n = 1
    do
        n = n + 1
        diff_table = [diff_table, int64_vec(diff(diff_table(n-1)%vals))] ! next line is diff of previous line
        if (all(diff_table(n)%vals==0)) exit
    end do

    ! extrapolate
    iextrap = 0
    do i = n-1, 1, -1
        associate( ilast => diff_table(i)%vals(size(diff_table(i)%vals)) )
            iextrap = iextrap + ilast
        end associate
    end do

    end function extrapolate

end program problem_9