problem_2 Program

Uses

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

Calls

program~~problem_2~~CallsGraph program~problem_2 problem_2 interface~split aoc_utilities::split program~problem_2->interface~split proc~clock_end aoc_utilities::clock%clock_end program~problem_2->proc~clock_end proc~clock_start aoc_utilities::clock%clock_start program~problem_2->proc~clock_start proc~number_of_lines_in_file aoc_utilities::number_of_lines_in_file program~problem_2->proc~number_of_lines_in_file proc~read_line aoc_utilities::read_line program~problem_2->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
logical :: status_ok
logical :: game_possible
character(len=:), allocatable :: line
character(len=:), allocatable :: color
integer :: n_lines
integer :: id
integer :: j
integer :: k
integer :: ipossible
integer :: inum
integer :: min_red
integer :: min_green
integer :: min_blue
integer :: power
type(string), dimension(:), allocatable :: vals
type(string), dimension(:), allocatable :: trys
type(string), dimension(:), allocatable :: cubes
type(string), dimension(:), allocatable :: num_color
integer, parameter :: n_red = 12
integer, parameter :: n_green = 13
integer, parameter :: n_blue = 14

Source Code

program problem_2

use iso_fortran_env
use aoc_utilities

implicit none

integer :: iunit
logical :: status_ok, game_possible
character(len=:),allocatable :: line, color
integer :: n_lines, id, j, k, ipossible, inum, min_red, min_green, min_blue, power
type(string),dimension(:),allocatable :: vals, trys, cubes, num_color

integer,parameter :: n_red   = 12
integer,parameter :: n_green = 13
integer,parameter :: n_blue  = 14

call clk%tic()

!example: Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
open(newunit=iunit, file='inputs/day2.txt', status='OLD')
n_lines = number_of_lines_in_file(iunit) ! will parse the line with successive splits
ipossible = 0
power = 0
main: do id = 1, n_lines
    game_possible = .true.
    min_red = 0; min_green = 0; min_blue = 0
    line = read_line(iunit,status_ok)
    vals = split(line,': ')
    trys = split(vals(2), '; ')
    do j = 1, size(trys)
        cubes = split(trys(j), ', ')
        do k = 1, size(cubes)
            num_color = split(cubes(k), ' ')
            inum = int(num_color(1)%str)
            color = num_color(2)%str
            select case (color)
            case('red')
                if (inum>n_red) game_possible = .false.
                if (inum>min_red) min_red = inum
            case('green')
                if (inum>n_green) game_possible = .false.
                if (inum>min_green) min_green = inum
            case('blue')
                if (inum>n_blue) game_possible = .false.
                if (inum>min_blue) min_blue = inum
            end select
        end do
    end do
    power = power + (min_red * min_green * min_blue)
    if (game_possible) ipossible = ipossible + id

end do main
write(*,*) '2a: result   :', ipossible
write(*,*) '2b: power sum:', power
close(iunit)

call clk%toc('2')

end program problem_2