problem_4 Program

Uses

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

Calls

program~~problem_4~~CallsGraph program~problem_4 problem_4 interface~split aoc_utilities::split program~problem_4->interface~split proc~clock_end aoc_utilities::clock%clock_end program~problem_4->proc~clock_end proc~clock_start aoc_utilities::clock%clock_start program~problem_4->proc~clock_start proc~number_of_lines_in_file aoc_utilities::number_of_lines_in_file program~problem_4->proc~number_of_lines_in_file proc~parse_ints aoc_utilities::parse_ints program~problem_4->proc~parse_ints proc~read_line aoc_utilities::read_line program~problem_4->proc~read_line 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 :: iunit
integer :: n_lines
integer :: i
integer :: points
integer :: j
integer :: iwin
type(string), dimension(:), allocatable :: vals
type(string), dimension(:), allocatable :: vals2
integer, dimension(:), allocatable :: iwinning
integer, dimension(:), allocatable :: ihave
integer, dimension(:), allocatable :: n_matches
character(len=:), allocatable :: line
integer, dimension(:,:), allocatable :: card_matrix

Source Code

program problem_4

use iso_fortran_env
use aoc_utilities

implicit none

integer :: iunit, n_lines, i, points, j, iwin
type(string),dimension(:),allocatable :: vals, vals2
integer,dimension(:),allocatable :: iwinning, ihave, n_matches
character(len=:),allocatable :: line
integer,dimension(:,:),allocatable :: card_matrix

call clk%tic()

! open(newunit=iunit, file='inputs/day4_test.txt', status='OLD')
open(newunit=iunit, file='inputs/day4.txt', status='OLD')
n_lines = number_of_lines_in_file(iunit)

allocate(n_matches(n_lines))              ! for part 2
allocate(card_matrix(0:n_lines, n_lines)) ! for part 2

! Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53
points = 0
do i = 1, n_lines
    line = read_line(iunit)
    vals  = split(line,': ')
    vals2 = split(vals(2), ' | ')
    iwinning = parse_ints(vals2(1)%str) ! winning list
    ihave    = parse_ints(vals2(2)%str) ! ones you have
    ! count the number of ones you have in the winning set:
    iwin = 0
    do j = 1, size(ihave)
        iwin = iwin + count(ihave(j) == iwinning)
    end do
    n_matches(i) = iwin
    points = points + 2**(iwin-1)
end do
close(iunit)
write(*,*) '4a : points : ', points

card_matrix = 0
card_matrix(0,:) = 1 ! start with one card each
do i = 1, n_lines
    associate (n => n_matches(i))
        if (n>0) card_matrix(i, i+1:i+n) = sum(card_matrix(:,i))
    end associate
end do
write(*,*) '4b : scratchcards :', sum(card_matrix)

call clk%toc('4')

end program problem_4