diff --git a/2023/Makefile b/2023/Makefile index 6dcc03e..dbcbe70 100644 --- a/2023/Makefile +++ b/2023/Makefile @@ -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 ./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 diff --git a/2023/data/day06.txt b/2023/data/day06.txt new file mode 100644 index 0000000..c78a7c2 --- /dev/null +++ b/2023/data/day06.txt @@ -0,0 +1,2 @@ +Time: 42 89 91 89 +Distance: 308 1170 1291 1467 diff --git a/2023/data/day06b.txt b/2023/data/day06b.txt new file mode 100644 index 0000000..3bfa331 --- /dev/null +++ b/2023/data/day06b.txt @@ -0,0 +1,2 @@ +Time: 42899189 +Distance: 308117012911467 diff --git a/2023/data/day06ex1.txt b/2023/data/day06ex1.txt new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/2023/data/day06ex1.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 diff --git a/2023/src/day06.f90 b/2023/src/day06.f90 new file mode 100644 index 0000000..82d4d48 --- /dev/null +++ b/2023/src/day06.f90 @@ -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