2023 day 1

main
Petra 2023-12-01 19:07:13 +13:00
parent 53b1c55e63
commit c95fc40a31
4 changed files with 1156 additions and 0 deletions

19
2023/Makefile 100644
View File

@ -0,0 +1,19 @@
FC:=gfortran
FFLAGS:=-Wall -Wno-maybe-uninitialized
BIN:=./bin
SRC:=./src
BINS:=./bin/day01.bin ./bin/day01b.bin
all: aoc19
$(SRC)/%.o: $(SRC)/%.f90
$(FC) $(FFLAGS) -c -o $@ $<
$(BIN)/%.bin: $(SRC)/%.o
$(FC) $(FFLAGS) -o $@ $<
aoc19: $(BINS)
clean:
rm -f $(BINS)

1001
2023/data/day01.txt 100644

File diff suppressed because it is too large Load Diff

53
2023/src/day01.f90 100644
View File

@ -0,0 +1,53 @@
program day1b
implicit none
character(300) :: data_line
character(1) :: test_char
integer :: num1, num2
integer :: numsum
integer :: running_total = 0
integer :: i, istat
integer :: i2, strlen
integer, parameter :: max_iterations = 10000
integer :: n_arguments
character(200) :: fname
n_arguments = command_argument_count()
if (n_arguments .eq. 1) then
call get_command_argument(1, fname)
print *, "File: ", fname
else
print *, "Wrong number of arguments: ", n_arguments
stop
end if
open(10, file=fname)
do i=1,10000
read(10, "(A)", iostat = istat) data_line
if (is_iostat_end(istat)) then
exit
end if
num1 = -1
num2 = -1
strlen = len_trim(data_line)
do i2=1,strlen
test_char = data_line(i2:i2)
if ((test_char .ge. '0') .and. (test_char .le. '9')) then
read(test_char, *) num2
if (num1 .eq. -1) then
num1 = num2
end if
end if
end do
if (num1 .ne. -1) then
numsum = num1 * 10 + num2
running_total = running_total + numsum
write(*, 20) num1, num2, numsum, data_line(1:strlen)
end if
end do
close(10)
print *
print *, "Sum of calibration values: ", running_total
20 format(i1, i2, i4, '| ', a)
end program day1b

View File

@ -0,0 +1,83 @@
program day1
implicit none
character(300) :: data_line
character(1) :: test_char
character(len=5) :: numnames(9)
integer :: num1, num2
integer :: numsum
integer :: running_total = 0
integer :: i, istat
integer :: i2, strlen, numlen, i3, i4
logical :: valid_number
integer, parameter :: max_iterations = 10000
integer :: n_arguments
character(200) :: fname
n_arguments = command_argument_count()
if (n_arguments .eq. 1) then
call get_command_argument(1, fname)
print *, "File: ", fname
else
print *, "Wrong number of arguments: ", n_arguments
stop
end if
numnames(1) = 'one'
numnames(2) = 'two'
numnames(3) = 'three'
numnames(4) = 'four'
numnames(5) = 'five'
numnames(6) = 'six'
numnames(7) = 'seven'
numnames(8) = 'eight'
numnames(9) = 'nine'
open(10, file=fname)
do i=1,10000
read(10, "(A)", iostat = istat) data_line
if (is_iostat_end(istat)) then
exit
end if
num1 = -1
num2 = -1
strlen = len_trim(data_line)
do i2=1,strlen
test_char = data_line(i2:i2)
if ((test_char .ge. '0') .and. (test_char .le. '9')) then
read(test_char, *) num2
if (num1 .eq. -1) then
num1 = num2
end if
else
do i3 = 1,9
numlen = len_trim(numnames(i3))
if ((numlen + i2 - 1) .le. strlen) then
valid_number = .true.
do i4=1,numlen
if (numnames(i3)(i4:i4) .ne. data_line((i2 + i4 - 1):(i2 + i4 - 1))) then
valid_number = .false.
exit
end if
end do
if (valid_number) then
num2 = i3
if (num1 .eq. -1) then
num1 = num2
end if
exit
end if
end if
end do
end if
end do
if (num1 .ne. -1) then
numsum = num1 * 10 + num2
running_total = running_total + numsum
write(*, 20) num1, num2, numsum, data_line(1:strlen)
end if
end do
close(10)
print *
print *, "Sum of calibration values: ", running_total
20 format(i1, i2, i4, '| ', a)
end program day1