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