2023 Day 6

main
Petra 2023-12-07 19:44:52 +13:00
parent 5c5dba9198
commit 36325259ba
5 changed files with 91 additions and 1 deletions

View File

@ -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 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
all: aoc19 all: aoc19

View File

@ -0,0 +1,2 @@
Time: 42 89 91 89
Distance: 308 1170 1291 1467

View File

@ -0,0 +1,2 @@
Time: 42899189
Distance: 308117012911467

View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

84
2023/src/day06.f90 100644
View File

@ -0,0 +1,84 @@
program day6
implicit none
integer, parameter :: ikind = selected_int_kind(12)
integer, parameter :: max_chars = 300
character(200) :: fname
character(max_chars) :: fline
integer :: n_arguments
integer(kind=ikind), allocatable :: times(:)
integer(kind=ikind), allocatable :: records(:)
integer :: pos_col, n_races, race
integer(kind=ikind) :: i, c_over, dist, win_mult = 1
print *, "Number of bits: ", ikind * 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)") fline
pos_col = scan(fline, ":")
n_races = num_ints(fline)
allocate(times(1:n_races))
allocate(records(1:n_races))
print *, "Number of races: ", n_races
read(fline((pos_col + 1):max_chars), *) times
read(10, "(A)") fline
pos_col = scan(fline, ":")
read(fline((pos_col + 1):max_chars), *) records
do race=1,n_races
c_over = 0
do i=1,times(race)-1
dist = i * (times(race) - i)
if (dist .gt. records(race)) then
c_over = c_over + 1
end if
end do
win_mult = win_mult * c_over
write(*, 20) race, c_over
end do
print *
print *, "Tolerance: ", win_mult
20 format("Race: ", i1, ". Ways to win: ", i12)
contains
function num_ints(str)
implicit none
character(*), intent(in) :: str
character(10), parameter :: digits = "0123456789"
integer :: num_ints
integer :: cur, cur0
integer :: str_len
cur = 0
cur0 = 0
num_ints = 0
str_len = len_trim(str)
do
cur = scan(str((cur + 1):str_len), digits) + cur0
if (cur .eq. cur0) then
return
end if
cur0 = cur
num_ints = num_ints + 1
if (cur + 1 .ge. str_len) then
return
end if
cur = verify(str((cur + 1):str_len), digits) + cur0
if (cur .eq. cur0) then
return
else if (cur + 1 .ge. str_len) then
return
end if
cur0 = cur
end do
end function num_ints
end program day6