2023 Day 15

main
Petra 2023-12-15 19:40:07 +13:00
parent 3dd7a42797
commit 8942f057f5
4 changed files with 117 additions and 1 deletions

View File

@ -2,7 +2,7 @@ FC:=gfortran
FFLAGS:=-Wall -Wno-maybe-uninitialized -O2
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 ./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 ./bin/day13.bin ./bin/day14.bin ./bin/day14b.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 ./bin/day13.bin ./bin/day14.bin ./bin/day14b.bin ./bin/day15.bin
all: aoc19

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

114
2023/src/day15.f90 100644
View File

@ -0,0 +1,114 @@
program day15
implicit none
integer, parameter :: max_chars = 100000
integer, parameter :: seq_chars = 100
integer, parameter :: max_seqs = 10000
character(200) :: fname
character(max_chars) :: fline
character(seq_chars) :: seqtext, label
integer :: n_arguments
integer :: istat
integer :: hashval, cpos_1, cpos_2, labend, hashtot, labhash
integer :: i, i2, strlen, n_placed, f_index, foc
logical :: found
integer :: boxno(1:max_seqs), focal(1:max_seqs), boxfound(0:255)
character(seq_chars) :: labels(1:max_seqs)
logical :: present(1:max_seqs)
integer :: focpow, totpow, tbox
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
close(10)
cpos_2 = 0
strlen = len_trim(fline)
hashtot = 0
present = .false.
n_placed = 0
do i=1,max_seqs
cpos_1 = cpos_2
cpos_2 = scan(fline((cpos_1+1):strlen), ',')
if (cpos_2 .eq. 0) then
cpos_2 = strlen + 1
else
cpos_2 = cpos_2 + cpos_1
end if
seqtext = fline((cpos_1+1):(cpos_2-1))
hashval = HASH(seqtext)
labend = scan(seqtext, '=-')
label = seqtext(1:(labend-1))
labhash = HASH(label)
print *, trim(seqtext), hashval, trim(label), labhash
hashtot = hashtot + hashval
found = .false.
do i2=1,n_placed
if ((boxno(i2) .eq. labhash) .and. (trim(labels(i2)) .eq. trim(label)) .and. present(i2)) then
found = .true.
f_index = i2
exit
end if
end do
if (seqtext(labend:labend) .eq. '-') then
if (found) then
present(f_index) = .false.
print *, "Delete ", trim(label), " at", boxno(f_index), f_index
else
print *, trim(label), " not found in box", labhash
end if
else
read(seqtext((labend+1):(labend+1)), *) foc
if (found) then
focal(f_index) = foc
print *, "Replace ", trim(label), " at", boxno(f_index), f_index
else
n_placed = n_placed + 1
boxno(n_placed) = labhash
labels(n_placed) = label
focal(n_placed) = foc
present(n_placed) = .true.
print *, "Place ", trim(label), ' at', labhash, n_placed, ' with', foc
end if
end if
if (cpos_2 .ge. strlen) then
exit
end if
end do
print *, "Total hash: ", hashtot
print *, "Number of lenses: ", i
boxfound = 0
totpow = 0
do i=1, n_placed
if (present(i)) then
tbox = boxno(i)
foc = focal(i)
boxfound(tbox) = boxfound(tbox) + 1
focpow = (tbox + 1) * boxfound(tbox) * foc
print *, tbox, boxfound(tbox), ' ', trim(labels(i)), foc, focpow
totpow = totpow + focpow
end if
end do
print *, "Total focal power: ", totpow
contains
function HASH(str)
implicit none
character(*), intent(in) :: str
integer :: HASH
integer :: i, strlen
strlen = len_trim(str)
HASH = 0
do i=1,strlen
HASH = mod((HASH + iachar(str(i:i))) * 17, 256)
end do
end function HASH
end program day15