go Subroutine

subroutine go(expand, isum)

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: expand
integer(kind=ip), intent(out) :: isum

Calls

proc~~go~8~~CallsGraph proc~go~8 problem_12::go interface~split aoc_utilities::split proc~go~8->interface~split proc~number_of_lines_in_file aoc_utilities::number_of_lines_in_file proc~go~8->proc~number_of_lines_in_file proc~parse_ints aoc_utilities::parse_ints proc~go~8->proc~parse_ints proc~read_line aoc_utilities::read_line proc~go~8->proc~read_line proc~str_to_int_array_with_mapping aoc_utilities::str_to_int_array_with_mapping proc~go~8->proc~str_to_int_array_with_mapping proc~test~2 problem_12::test proc~go~8->proc~test~2 proc~split1 aoc_utilities::split1 interface~split->proc~split1 proc~split2 aoc_utilities::split2 interface~split->proc~split2 proc~test~2->proc~test~2 proc~match problem_12::match proc~test~2->proc~match proc~expand_vector aoc_utilities::expand_vector proc~split1->proc~expand_vector proc~split2->proc~split1

Called by

proc~~go~8~~CalledByGraph proc~go~8 problem_12::go program~problem_12 problem_12 program~problem_12->proc~go~8

Source Code

subroutine go(expand,isum)
    logical,intent(in) :: expand
    integer(ip),intent(out) :: isum

    integer :: iunit, n_lines, n_unknowns
    integer(ip) :: n_perms
    character(len=:),allocatable :: line, pattern
    type(string),dimension(:),allocatable :: vals

    ! open(newunit=iunit, file='inputs/day12_test.txt', status='OLD')
    open(newunit=iunit, file='inputs/day12.txt', status='OLD')
    n_lines = number_of_lines_in_file(iunit)
    isum = 0
    do iline = 1, n_lines
        line = read_line(iunit)
        vals = split(line,' ')
        ints = parse_ints(vals(2)%str)     ! integer list  1,1,3
        pattern = vals(1)%str              ! the pattern   #.#.###

        ! will convert the pattern to an array of numbers:
        ipattern = str_to_int_array_with_mapping(pattern,['.','#','?'],&
                                                        [POINT,NUMBER,QUESTION])  ! 1010111

        if (expand) then
            ! brute force it
            ipattern = [ipattern, QUESTION, ipattern, QUESTION, &
                        ipattern, QUESTION, ipattern, QUESTION, &
                        ipattern]
            ints = [ints, ints, ints, ints, ints]
        end if

        n_unknowns = count(ipattern==2)
        n_valid = 0 ! number of valid permutations
        n_perms = 2 ** n_unknowns ! number of permutations
        ipattern_tmp = ipattern

        ! recursively test all the permutations
        if (allocated(a)) deallocate(a)
        allocate(a(n_unknowns))
        call test(1, n_unknowns)

        isum = isum + n_valid

       ! write(*,*) iline, 'n_valid = ', n_valid

    end do

    end subroutine go