2023 Day 12 part 1
parent
97d6c075f3
commit
f20eb10586
|
@ -2,7 +2,7 @@ FC:=gfortran
|
||||||
FFLAGS:=-Wall -Wno-maybe-uninitialized
|
FFLAGS:=-Wall -Wno-maybe-uninitialized
|
||||||
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
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,7 @@
|
||||||
|
???.### 1,1,3
|
||||||
|
.??..??...?##. 1,1,3
|
||||||
|
?#?#?#?#?#?#?#? 1,3,1,6
|
||||||
|
????.#...#... 4,1,1
|
||||||
|
????.######..#####. 1,6,5
|
||||||
|
?###???????? 3,2,1
|
||||||
|
|
|
@ -0,0 +1,157 @@
|
||||||
|
program day12
|
||||||
|
implicit none
|
||||||
|
integer, parameter :: max_chars = 300
|
||||||
|
integer, parameter :: max_rows = 2000
|
||||||
|
character(200) :: fname
|
||||||
|
character(max_chars) :: fline
|
||||||
|
character(max_chars) :: springs
|
||||||
|
integer :: n_springs
|
||||||
|
integer :: n_arguments
|
||||||
|
integer :: istat
|
||||||
|
integer :: strlen
|
||||||
|
integer, allocatable :: specifiers(:)
|
||||||
|
integer :: n_specifiers
|
||||||
|
integer :: i, i2
|
||||||
|
integer :: s_pos
|
||||||
|
integer :: val, total_valid
|
||||||
|
|
||||||
|
n_arguments = command_argument_count()
|
||||||
|
if (n_arguments .eq. 1) then
|
||||||
|
call get_command_argument(1, fname)
|
||||||
|
print *, "File: ", trim(fname)
|
||||||
|
print *
|
||||||
|
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, " ")
|
||||||
|
n_springs = s_pos - 1
|
||||||
|
springs = fline(1:n_springs)
|
||||||
|
n_specifiers = 1
|
||||||
|
do i2 = s_pos+1, strlen
|
||||||
|
if (fline(i2:i2) .eq. ',') then
|
||||||
|
n_specifiers = n_specifiers + 1
|
||||||
|
end if
|
||||||
|
end do
|
||||||
|
allocate(specifiers(1:n_specifiers))
|
||||||
|
read(fline((s_pos + 1):strlen), *) specifiers
|
||||||
|
print *, trim(springs)
|
||||||
|
call set3(springs)
|
||||||
|
val = 0
|
||||||
|
do
|
||||||
|
if (consistent(springs, n_springs, specifiers, n_specifiers)) then
|
||||||
|
val = val + 1
|
||||||
|
print *, trim(springs)
|
||||||
|
end if
|
||||||
|
if (no3(springs)) then
|
||||||
|
exit
|
||||||
|
end if
|
||||||
|
call iterate(springs, strlen)
|
||||||
|
end do
|
||||||
|
print *, "Valid: ", val
|
||||||
|
print *
|
||||||
|
total_valid = total_valid + val
|
||||||
|
deallocate(specifiers)
|
||||||
|
end do
|
||||||
|
close(10)
|
||||||
|
print *
|
||||||
|
print *, "Total valid: ", total_valid
|
||||||
|
|
||||||
|
contains
|
||||||
|
subroutine set3(springs)
|
||||||
|
character(*), intent(inout) :: springs
|
||||||
|
integer :: pos
|
||||||
|
do
|
||||||
|
pos = scan(springs, '?')
|
||||||
|
if (pos .eq. 0) then
|
||||||
|
exit
|
||||||
|
end if
|
||||||
|
springs(pos:pos) = '3'
|
||||||
|
end do
|
||||||
|
end subroutine
|
||||||
|
|
||||||
|
subroutine iterate(springs, nspr)
|
||||||
|
character(*), intent(inout) :: springs
|
||||||
|
integer, intent(in) :: nspr
|
||||||
|
integer :: p1, p2
|
||||||
|
p1 = 1
|
||||||
|
do
|
||||||
|
if (p1 .gt. nspr) then
|
||||||
|
exit
|
||||||
|
end if
|
||||||
|
p2 = scan(springs(p1:nspr), '3>')
|
||||||
|
if (p2 .eq. 0) then
|
||||||
|
exit
|
||||||
|
end if
|
||||||
|
p2 = p2 + p1 - 1
|
||||||
|
if (springs(p2:p2) .eq. '3') then
|
||||||
|
springs(p2:p2) = '>'
|
||||||
|
exit
|
||||||
|
else
|
||||||
|
springs(p2:p2) = '3'
|
||||||
|
! carry the '>'
|
||||||
|
p1 = p2 + 1
|
||||||
|
end if
|
||||||
|
end do
|
||||||
|
end subroutine
|
||||||
|
|
||||||
|
function no3(springs)
|
||||||
|
character(*), intent(in) :: springs
|
||||||
|
logical :: no3
|
||||||
|
no3 = (scan(springs, '3') .eq. 0)
|
||||||
|
end function no3
|
||||||
|
|
||||||
|
function consistent(springs, n_spr, specs, n_specs)
|
||||||
|
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), '#3')
|
||||||
|
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), '#3')
|
||||||
|
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
|
||||||
|
end program day12
|
||||||
|
|
Loading…
Reference in New Issue