Compare commits
	
		
			2 Commits
		
	
	
		
			97d6c075f3
			...
			3477b81b21
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					3477b81b21 | ||
| 
						 | 
					f20eb10586 | 
@ -1,8 +1,8 @@
 | 
				
			|||||||
FC:=gfortran
 | 
					FC:=gfortran
 | 
				
			||||||
FFLAGS:=-Wall -Wno-maybe-uninitialized
 | 
					FFLAGS:=-Wall -Wno-maybe-uninitialized -O2
 | 
				
			||||||
BIN:=./bin
 | 
					BIN:=./bin
 | 
				
			||||||
SRC:=./src
 | 
					SRC:=./src
 | 
				
			||||||
BINS:=./bin/day01.bin ./bin/day01b.bin ./bin/day02.bin ./bin/day03.bin ./bin/day04.bin ./bin/day05.bin ./bin/day05b.bin ./bin/day06.bin ./bin/day07.bin ./bin/day07b.bin ./bin/day08.bin ./bin/day08b.bin ./bin/day09.bin ./bin/day10.bin ./bin/day11.bin
 | 
					BINS:=./bin/day01.bin ./bin/day01b.bin ./bin/day02.bin ./bin/day03.bin ./bin/day04.bin ./bin/day05.bin ./bin/day05b.bin ./bin/day06.bin ./bin/day07.bin ./bin/day07b.bin ./bin/day08.bin ./bin/day08b.bin ./bin/day09.bin ./bin/day10.bin ./bin/day11.bin ./bin/day12.bin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
all: aoc19
 | 
					all: aoc19
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										1001
									
								
								2023/data/day12.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1001
									
								
								2023/data/day12.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										7
									
								
								2023/data/day12ex1.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								2023/data/day12ex1.txt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					???.### 1,1,3
 | 
				
			||||||
 | 
					.??..??...?##. 1,1,3
 | 
				
			||||||
 | 
					?#?#?#?#?#?#?#? 1,3,1,6
 | 
				
			||||||
 | 
					????.#...#... 4,1,1
 | 
				
			||||||
 | 
					????.######..#####. 1,6,5
 | 
				
			||||||
 | 
					?###???????? 3,2,1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										225
									
								
								2023/src/day12.f90
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										225
									
								
								2023/src/day12.f90
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,225 @@
 | 
				
			|||||||
 | 
					program day12
 | 
				
			||||||
 | 
					    implicit none
 | 
				
			||||||
 | 
					    integer, parameter :: max_chars = 300
 | 
				
			||||||
 | 
					    integer, parameter :: max_rows = 2000
 | 
				
			||||||
 | 
					    integer, parameter :: b_kind = selected_int_kind(15)
 | 
				
			||||||
 | 
					    character(200) :: fname
 | 
				
			||||||
 | 
					    character(max_chars) :: fline
 | 
				
			||||||
 | 
					    character(max_chars) :: springs, spt
 | 
				
			||||||
 | 
					    integer :: tn_spr, n_springs
 | 
				
			||||||
 | 
					    integer :: n_arguments
 | 
				
			||||||
 | 
					    integer :: n_repeats
 | 
				
			||||||
 | 
					    integer :: istat
 | 
				
			||||||
 | 
					    integer :: strlen
 | 
				
			||||||
 | 
					    integer, allocatable :: tspec(:), specifiers(:)
 | 
				
			||||||
 | 
					    integer :: tn_spec, n_specifiers
 | 
				
			||||||
 | 
					    integer :: i, i2, i3
 | 
				
			||||||
 | 
					    integer :: s_pos
 | 
				
			||||||
 | 
					    integer :: num_wc
 | 
				
			||||||
 | 
					    integer(kind=b_kind) :: val, total_valid
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    n_arguments = command_argument_count()
 | 
				
			||||||
 | 
					    if (n_arguments .eq. 2) then
 | 
				
			||||||
 | 
					        call get_command_argument(1, fname)
 | 
				
			||||||
 | 
					        print *, "File: ", trim(fname)
 | 
				
			||||||
 | 
					        call get_command_argument(2, fline)
 | 
				
			||||||
 | 
					        read(fline, *) n_repeats
 | 
				
			||||||
 | 
					        print *, "Number of repeats: ", n_repeats
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        print *, "Wrong number of arguments: ", n_arguments
 | 
				
			||||||
 | 
					        stop
 | 
				
			||||||
 | 
					    end if
 | 
				
			||||||
 | 
					    open(10, file=fname)
 | 
				
			||||||
 | 
					    total_valid = 0
 | 
				
			||||||
 | 
					    do i=1,max_rows
 | 
				
			||||||
 | 
					        read(10, "(a)", iostat=istat) fline
 | 
				
			||||||
 | 
					        strlen = len_trim(fline)
 | 
				
			||||||
 | 
					        if ((strlen .eq. 0) .or. (is_iostat_end(istat))) then
 | 
				
			||||||
 | 
					            exit
 | 
				
			||||||
 | 
					        end if
 | 
				
			||||||
 | 
					        s_pos = scan(fline, " ")
 | 
				
			||||||
 | 
					        tn_spr = s_pos - 1
 | 
				
			||||||
 | 
					        spt = fline(1:tn_spr)
 | 
				
			||||||
 | 
					        tn_spec = 1
 | 
				
			||||||
 | 
					        do i2 = s_pos+1, strlen
 | 
				
			||||||
 | 
					            if (fline(i2:i2) .eq. ',') then
 | 
				
			||||||
 | 
					                tn_spec = tn_spec + 1
 | 
				
			||||||
 | 
					            end if
 | 
				
			||||||
 | 
					        end do
 | 
				
			||||||
 | 
					        n_specifiers = tn_spec * (1 + n_repeats)
 | 
				
			||||||
 | 
					        allocate(tspec(1:tn_spec))
 | 
				
			||||||
 | 
					        allocate(specifiers(1:n_specifiers))
 | 
				
			||||||
 | 
					        read(fline((s_pos + 1):strlen), *) tspec
 | 
				
			||||||
 | 
					        do i2=0,n_repeats
 | 
				
			||||||
 | 
					            do i3=1,tn_spec
 | 
				
			||||||
 | 
					                specifiers(i2 * tn_spec + i3) = tspec(i3)
 | 
				
			||||||
 | 
					            end do
 | 
				
			||||||
 | 
					        end do
 | 
				
			||||||
 | 
					        springs = spt
 | 
				
			||||||
 | 
					        if (n_repeats .ge. 1) then
 | 
				
			||||||
 | 
					            do i2=1,n_repeats
 | 
				
			||||||
 | 
					                springs = trim(springs) // '?' // trim(spt)
 | 
				
			||||||
 | 
					            end do
 | 
				
			||||||
 | 
					        end if
 | 
				
			||||||
 | 
					        n_springs = (n_repeats + 1) * tn_spr + n_repeats
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        print *, "Row: ", i
 | 
				
			||||||
 | 
					        print *, "Original: ", trim(spt)
 | 
				
			||||||
 | 
					        print *, "Original specifiers: "
 | 
				
			||||||
 | 
					        print "(20i2)", tspec
 | 
				
			||||||
 | 
					        print *, "Expanded: ", trim(springs)
 | 
				
			||||||
 | 
					        print *, "Expanded specifiers: "
 | 
				
			||||||
 | 
					        print "(20i2)", specifiers
 | 
				
			||||||
 | 
					        num_wc = numstr(trim(springs), '?')
 | 
				
			||||||
 | 
					        print *, "Length: ", n_springs
 | 
				
			||||||
 | 
					        print *, "Number of wildcards: ", num_wc
 | 
				
			||||||
 | 
					        val = nvalid(springs, n_springs, specifiers, n_specifiers, .true.)
 | 
				
			||||||
 | 
					        print *, "Valid: ", val
 | 
				
			||||||
 | 
					        print *
 | 
				
			||||||
 | 
					        total_valid = total_valid + val
 | 
				
			||||||
 | 
					        deallocate(tspec)
 | 
				
			||||||
 | 
					        deallocate(specifiers)
 | 
				
			||||||
 | 
					    end do
 | 
				
			||||||
 | 
					    close(10)
 | 
				
			||||||
 | 
					    print *
 | 
				
			||||||
 | 
					    print *, "Total valid: ", total_valid
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    contains
 | 
				
			||||||
 | 
					        function consistent(springs, n_spr, specs, n_specs)
 | 
				
			||||||
 | 
					            implicit none
 | 
				
			||||||
 | 
					            character(*), intent(in) ::springs
 | 
				
			||||||
 | 
					            integer, intent(in) :: specs(:)
 | 
				
			||||||
 | 
					            integer, intent(in) :: n_spr, n_specs
 | 
				
			||||||
 | 
					            logical :: consistent
 | 
				
			||||||
 | 
					            integer :: c1, c2, c3, i, speccur, clen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            consistent = .true.
 | 
				
			||||||
 | 
					            c1 = 1
 | 
				
			||||||
 | 
					            speccur = 0
 | 
				
			||||||
 | 
					            do i = 1, n_spr
 | 
				
			||||||
 | 
					                if (c1 .gt. n_spr) then
 | 
				
			||||||
 | 
					                    consistent = speccur .eq. n_specs
 | 
				
			||||||
 | 
					                    ! We've run out of springs
 | 
				
			||||||
 | 
					                    return
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					                c2 = scan(springs(c1:n_spr), '#')
 | 
				
			||||||
 | 
					                if (c2 .eq. 0) then
 | 
				
			||||||
 | 
					                    consistent = speccur .eq. n_specs
 | 
				
			||||||
 | 
					                    ! Also run out of springs
 | 
				
			||||||
 | 
					                    return
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					                c2 = c1 + c2 - 1
 | 
				
			||||||
 | 
					                c3 = verify(springs(c2:n_spr), '#')
 | 
				
			||||||
 | 
					                if (c3 .eq. 0) then
 | 
				
			||||||
 | 
					                    ! Ends at end of string
 | 
				
			||||||
 | 
					                    c3 = n_spr
 | 
				
			||||||
 | 
					                else
 | 
				
			||||||
 | 
					                    c3 = c2 + c3 - 2
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					                speccur = speccur + 1
 | 
				
			||||||
 | 
					                if (speccur .gt. n_specs) then
 | 
				
			||||||
 | 
					                    consistent = .false.
 | 
				
			||||||
 | 
					                    return
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					                clen = c3 - c2 + 1
 | 
				
			||||||
 | 
					                if (clen .ne. specs(speccur)) then
 | 
				
			||||||
 | 
					                    consistent = .false.
 | 
				
			||||||
 | 
					                    return
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					                c1 = c3 + 1
 | 
				
			||||||
 | 
					            end do
 | 
				
			||||||
 | 
					            print *, '???'
 | 
				
			||||||
 | 
					        end function consistent
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        function numstr(springs, str)
 | 
				
			||||||
 | 
					            implicit none
 | 
				
			||||||
 | 
					            character(*), intent(in) :: springs, str
 | 
				
			||||||
 | 
					            integer :: numstr
 | 
				
			||||||
 | 
					            integer :: sprlen, i
 | 
				
			||||||
 | 
					            sprlen = len_trim(springs)
 | 
				
			||||||
 | 
					            numstr = 0
 | 
				
			||||||
 | 
					            do i=1,sprlen
 | 
				
			||||||
 | 
					                if (scan(springs(i:i), str) .ne. 0) then
 | 
				
			||||||
 | 
					                    numstr = numstr + 1
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					            end do
 | 
				
			||||||
 | 
					        end function numstr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        function viable(springs, n_spr, specs, n_specs)
 | 
				
			||||||
 | 
					            implicit none
 | 
				
			||||||
 | 
					            character(*), intent(in) ::springs
 | 
				
			||||||
 | 
					            integer, intent(in) :: specs(:)
 | 
				
			||||||
 | 
					            integer, intent(in) :: n_spr, n_specs
 | 
				
			||||||
 | 
					            logical :: viable
 | 
				
			||||||
 | 
					            character(len=1) :: ch
 | 
				
			||||||
 | 
					            integer :: i, i2, spi, spc, nrq, remt
 | 
				
			||||||
 | 
					            logical :: ch1v, ch2v
 | 
				
			||||||
 | 
					            spi = 0
 | 
				
			||||||
 | 
					            spc = 0
 | 
				
			||||||
 | 
					            viable = .true.
 | 
				
			||||||
 | 
					            ch2v = .false.
 | 
				
			||||||
 | 
					            do i=1,n_spr
 | 
				
			||||||
 | 
					                ch1v = ch2v
 | 
				
			||||||
 | 
					                ch = springs(i:i)
 | 
				
			||||||
 | 
					                if (ch .eq. '?') then
 | 
				
			||||||
 | 
					                    nrq = numstr(springs(i:n_spr), '?#')
 | 
				
			||||||
 | 
					                    remt = 0
 | 
				
			||||||
 | 
					                    do i2=spi,n_specs
 | 
				
			||||||
 | 
					                        remt = remt + specs(i2)
 | 
				
			||||||
 | 
					                    end do
 | 
				
			||||||
 | 
					                    remt = remt - spc
 | 
				
			||||||
 | 
					                    viable = (nrq .ge. remt)
 | 
				
			||||||
 | 
					                    return
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					                ch2v = scan(ch, '#') .ne. 0
 | 
				
			||||||
 | 
					                if (ch2v) then
 | 
				
			||||||
 | 
					                    if (ch1v) then
 | 
				
			||||||
 | 
					                        spc = spc + 1
 | 
				
			||||||
 | 
					                        if (spc .gt. specs(spi)) then
 | 
				
			||||||
 | 
					                            viable = .false.
 | 
				
			||||||
 | 
					                            return
 | 
				
			||||||
 | 
					                        end if
 | 
				
			||||||
 | 
					                    else
 | 
				
			||||||
 | 
					                        spc = 1
 | 
				
			||||||
 | 
					                        spi = spi + 1
 | 
				
			||||||
 | 
					                        if (spi .gt. n_specs) then
 | 
				
			||||||
 | 
					                            viable = .false.
 | 
				
			||||||
 | 
					                            return
 | 
				
			||||||
 | 
					                        end if
 | 
				
			||||||
 | 
					                    end if
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					            end do
 | 
				
			||||||
 | 
					        end function viable
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        recursive function nvalid(springs, n_spr, specs, n_specs, r_branch) result(nv)
 | 
				
			||||||
 | 
					            implicit none
 | 
				
			||||||
 | 
					            character(len=max_chars), intent(in) ::springs
 | 
				
			||||||
 | 
					            integer, intent(in) :: specs(:)
 | 
				
			||||||
 | 
					            integer, intent(in) :: n_spr, n_specs
 | 
				
			||||||
 | 
					            logical, intent(in) :: r_branch
 | 
				
			||||||
 | 
					            integer :: nv
 | 
				
			||||||
 | 
					            integer :: s_pos
 | 
				
			||||||
 | 
					            character(len=max_chars) :: test_spr
 | 
				
			||||||
 | 
					            nv = 0
 | 
				
			||||||
 | 
					            s_pos = scan(springs, '?')
 | 
				
			||||||
 | 
					            if (r_branch) then
 | 
				
			||||||
 | 
					                print *, "Right branch depth: ", s_pos
 | 
				
			||||||
 | 
					            end if
 | 
				
			||||||
 | 
					            if (s_pos .eq. 0) then
 | 
				
			||||||
 | 
					                if (consistent(springs, n_spr, specs, n_specs)) then
 | 
				
			||||||
 | 
					                    nv = nv + 1
 | 
				
			||||||
 | 
					                end if
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					            end if
 | 
				
			||||||
 | 
					            test_spr = springs
 | 
				
			||||||
 | 
					            test_spr(s_pos:s_pos) = '#'
 | 
				
			||||||
 | 
					            if (viable(test_spr, n_spr, specs, n_specs)) then
 | 
				
			||||||
 | 
					                nv = nv + nvalid(test_spr, n_spr, specs, n_specs, .false.)
 | 
				
			||||||
 | 
					            end if
 | 
				
			||||||
 | 
					            test_spr(s_pos:s_pos) = '.'
 | 
				
			||||||
 | 
					            if (viable(test_spr, n_spr, specs, n_specs)) then
 | 
				
			||||||
 | 
					                nv = nv + nvalid(test_spr, n_spr, specs, n_specs, r_branch)
 | 
				
			||||||
 | 
					            end if
 | 
				
			||||||
 | 
					        end function nvalid
 | 
				
			||||||
 | 
					end program day12
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user