Finish 2023 day 5

main
Petra 2023-12-06 21:18:01 +13:00
parent 2caad038bc
commit 6fbf94efc8
2 changed files with 175 additions and 1 deletions

View File

@ -2,7 +2,7 @@ FC:=gfortran
FFLAGS:=-Wall -Wno-maybe-uninitialized
BIN:=./bin
SRC:=./src
BINS:=./bin/day01.bin ./bin/day01b.bin ./bin/day02.bin ./bin/day03.bin ./bin/day04.bin ./bin/day05.bin
BINS:=./bin/day01.bin ./bin/day01b.bin ./bin/day02.bin ./bin/day03.bin ./bin/day04.bin ./bin/day05.bin ./bin/day05b.bin
all: aoc19

174
2023/src/day05b.f90 100644
View File

@ -0,0 +1,174 @@
program day5b
implicit none
integer, parameter :: i_kind = selected_int_kind(20)
integer, parameter :: max_chars = 300
character(200) :: fname
character(max_chars) :: fline
integer :: n_arguments
integer :: istat
integer(kind=i_kind), allocatable :: seedvals(:)
integer :: n_seeddefs, num_chars, pos_col
integer :: i
integer, parameter :: max_defs = 100
integer(kind=i_kind) :: t1_defs(1:2, 1:max_defs)
integer(kind=i_kind) :: t2_defs(1:2, 1:max_defs)
integer :: t1_i, t2_i, t1_n, t2_n
integer :: sec
integer, parameter :: max_iter = 20
integer(kind=i_kind) :: ldat(1:3)
integer(kind=i_kind) :: sw_l, sw_h, offset
integer(kind=i_kind) :: c_l, c_h
integer(kind=i_kind) :: min_loc_val
print *, "Number of bits: ", i_kind * 8
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)
read(10, "(A)", iostat = istat) fline
pos_col = scan(fline, ":")
n_seeddefs = 0
num_chars = len_trim(fline)
do i=pos_col + 1,num_chars
if (fline(i:i) .eq. ' ') then
n_seeddefs = n_seeddefs + 1
end if
end do
print *, "Number of seed definitions: ", n_seeddefs
allocate(seedvals(1:n_seeddefs))
read(fline((pos_col + 1):(num_chars)), *) seedvals
print *
print "(2i11)", seedvals
print *
do t2_i=1,n_seeddefs/2
t2_defs(1,t2_i) = seedvals(t2_i * 2 - 1)
t2_defs(2,t2_i) = seedvals(t2_i * 2 - 1) + seedvals(t2_i * 2) - 1
end do
t2_n = n_seeddefs/2
print "(2i11)", t2_defs(1:2, 1:t2_n)
read(10, "(A)", iostat = istat) fline
do sec=1, max_iter
t1_defs = t2_defs
t2_defs = -1
t1_n = t2_n
t2_n = 0
t2_i = 0
read(10, "(A)", iostat = istat) fline
if (is_iostat_end(istat)) then
exit
end if
do
read(10, "(A)", iostat = istat) fline
num_chars = len_trim(fline)
if (is_iostat_end(istat) .or. (num_chars .eq. 0)) then
exit
end if
read(fline, *) ldat
sw_l = ldat(2)
sw_h = ldat(2) + ldat(3) - 1
offset = ldat(1) - ldat(2)
do t1_i=1,t1_n
c_l = t1_defs(1,t1_i)
c_h = t1_defs(2,t1_i)
if ((c_l .eq. -1) .and. (c_h .eq. -1)) then
! Already dealt with
continue
else if ((sw_h .lt. c_l) .or. (sw_l .gt. c_h)) then
! Not in range
continue
else if ((c_l .ge. sw_l) .and. (c_h .le. sw_h)) then
! Entirely contained
t2_i = t2_i + 1
if (t2_i .gt. max_defs) then
print *, "Overflow t2"
end if
t2_defs(1, t2_i) = c_l + offset
t2_defs(2, t2_i) = c_h + offset
t1_defs(1:2, t1_i) = -1
t2_n = t2_n + 1
else if ((c_l .lt. sw_l) .and. (c_h .le. sw_h)) then
! Overlap - low
t2_i = t2_i + 1
if (t2_i .gt. max_defs) then
print *, "Overflow t2"
end if
t2_defs(1, t2_i) = sw_l + offset
t2_defs(2, t2_i) = c_h + offset
t1_defs(1, t1_i) = c_l ! No change
t1_defs(2, t1_i) = sw_l - 1
t2_n = t2_n + 1
else if ((c_l .ge. sw_l) .and. (c_h .gt. sw_h)) then
! Overlap - high
t2_i = t2_i + 1
if (t2_i .gt. max_defs) then
print *, "Overflow t2"
end if
t2_defs(1, t2_i) = c_l + offset
t2_defs(2, t2_i) = sw_h + offset
t1_defs(1, t1_i) = sw_h + 1
t1_defs(2, t1_i) = c_h ! No change
t2_n = t2_n + 1
else if ((c_l .lt. sw_l) .and. (c_h .gt. sw_h)) then
! Entirely contained by
t2_i = t2_i + 1
if (t2_i .gt. max_defs) then
print *, "Overflow t2"
end if
t2_defs(1, t2_i) = sw_l + offset
t2_defs(2, t2_i) = sw_h + offset
t1_defs(1, t1_i) = c_l ! No change
t1_defs(2, t1_i) = sw_l - 1
t1_n = t1_n + 1
if (t1_i .gt. max_defs) then
print *, "Overflow t1"
end if
t1_defs(1, t1_n) = sw_h + 1
t1_defs(2, t1_n) = c_h
else
! ??
print *, "Unclear situation"
stop
end if
end do
end do
do t1_i=1,t1_n
c_l = t1_defs(1,t1_i)
c_h = t1_defs(2,t1_i)
if ((c_l .ne. -1) .or. (c_h .ne. -1)) then
t2_i = t2_i + 1
if (t2_i .gt. max_defs) then
print *, "Overflow t2"
end if
t2_defs(1, t2_i) = c_l
t2_defs(2, t2_i) = c_h
t2_n = t2_n + 1
end if
end do
print *
print *, "Ranges after section ", sec
print "(2i11)", t2_defs(1:2, 1:t2_n)
if (is_iostat_end(istat)) then
exit
end if
end do
min_loc_val = huge(min_loc_val)
do t2_i=1,t2_n
if (t2_defs(1, t2_i) .lt. min_loc_val) then
min_loc_val = t2_defs(1, t2_i)
end if
end do
print "(a, i11)", "Lowest location value: ", min_loc_val
close(10)
deallocate(seedvals)
end program day5b