2023 Day 16

main
Petra 2023-12-16 19:28:06 +13:00
parent a0efdb9cce
commit b0e4b21c73
4 changed files with 326 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 ./bin/day15.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 ./bin/day16.bin
all: aoc19

111
2023/data/day16.txt 100644
View File

@ -0,0 +1,111 @@
\../....|.-........|.................|...\....../...............\...............|..................\........|.
.-...../.......-.......|./.........-.....-.......\...|.-...........-.......\.................-....-...........
.\....-\..|...\.....\...................................................................../................-..
.....|.............-.\....../......|.....\......................|...-.....-.........|.-......\................
........./............................-.|..|.........|.....|..................................................
................./.................|.................\...............\..................|.../.....././........
...-..|.........-.............\..|..-......./............\.-....|..|........\...-......\....../...\\..........
..............\..|............/..............|..........................................|............\........
.|...........................-.-.........-..\|...|.......|\............\|.....................................
..........-./.\.....|...................|./............./.-..............|........../.....-/..................
....../.....\/..........\.........|\...........|....--................/................../......-..........-..
-.|.......|...................../................\..................-\........-.....\.........................
...-|................................................\...........-.........\/.-......-.............|..........
.......\../....\..............|..........|.....\............|.............|......./................./....../\.
.....|..................|...|......-.....|..-....|\\.........\\........|....................|............-....
......./........./...-..-|................../...|....................|-.......\-............................./
................../......-...........|...|........./.....\..|......../..../.......|...|.................-.....
.................................../.|..........|...............\..........|...............-.............|/...
.......\.-......-................................/......|..\...\.........../........|......\.........-..-....\
....................|./.....-..../.............|...............-.../.\........-..-............/....|.......-..
...........\...........................\.|.................-...............|........-....................|..|.
..|.-.....\.......|................-.........../.............................................|....//.\........
..-...............\......../..||.....|.....\.|.........................\.....|....-.........../..../..........
..|......../.../.............|..........-...................-................\...\..\......-..................
...../...........-..../...|..\...././..\.|............\....................-|......../........................
/..|............-\..|.................../.../..................../...-........|...............-../......|-....
................/..............................................|.|......-...................|.................
....\..../.../.................\\............................................\........\................../..|.
../.-.............|......|.........\.........-\........../..........|.\.......................................
................./................./\......./........-.........................../........-..........|....\.\.
..\...../............/.............................................-.......--...|.................|...../...-.
..\......................-.......................-......-.\.-/../......./...........................-../......
...............-.................../................../.................-.........................|....\....|.
..../.\|..............-...../...|...../................\-....\..\........|....................-...............
.........................\...-...-|...................\....../........|...............-....../.../......./....
....\......../.-....-........-...../.....-.......-.\...............\.........\............./.....\-...|-..-|..
.../\................-....\.......|..-............-.\.............\..\........-............/......./......../.
..\.....|...............|..\......|..................................|......./.....\...|..|....\../......./...
......|.....././.....-..\..........................-\.............|..../.................................-/...
................|\.......|...|.|....................\..............\........................\.-...||..........
.-.......\...........-...../............/--....-..\-......................./...-.....|..........\/.....-......
...-............-.........\.....................|.........../.........../............|.-.|.....\.../...\-.....
.......................|...........\................./...................\............................|.......
......................|..|.\...............-.../................/.../.../...........................-.|.......
.\.|..........-.|................................../................................/.........-.\....\../.....
./............\..\............................|....../........\.........................\.......\..../.\.....-
....................../.....................|......./....../............................\....................|
..............................||....\........-..............................\..........\\........./.../.......
....\....../....................................-..-........................-................-....|.........\.
.......-......\.-\.|............................................/......-......../.......\.....................
....../...................|........-....-........./...........-.-........./....\..............................
..-/..............\............|........../....................\........-.\...................................
...-......\............/\...........\.........................-.......\.-./...................................
..........-.\.//......................--............\....../.........../-./...................................
.......\-.-..-.....\.............|............\....../........\......../...............-..............|....-..
........./.-.............\.........................-....|\..........|..../...-.........................\.\...\
.........|..\.|.........-...-........-./............|......|....................|.........../...|.............
................./....-..../\..............|../-................|.../..............-./|..-........||..........
...../.......-.............|.........-.....|.............................\........\.........\.................
.............|...../...................../.................\\.....................|\..........-....././......-
.............../..../..../....................../...\.......-..........................\\...-|.........../....
...........|........|........../..-........../......\...........-.......-.......-.......................-..|..
........................\...../.........-.........\.....|...|.........-.....-................-|.../...........
..-...............|...-...........\|.\.......\..............................\./......./..........-............
........................../....../....................................|................................../....
/.......\.................................\./............................|...\..............|...-............|
.....-.....................|.........|................-.\.....-..../..../...............-.........|...........
...|................\../..|.................|............/.....-......../.........-...\-.-.......\............
........................\....-..........|..-....../..............-..........-..|.........\.-...........\......
................................\\......./...................-...........|..................\....|...|../.....
......-.-.|...\.........|.........|....\......................................................................
...................-/....\......../...........|................/......|.-........\|./..|......../.....|..\....
....../........-...\../../..........|...\.....\.............................../......-........-...............
............\...-....|...................\............-.............|..-......|..../-.........................
...........././.-......./............../.....\...-..-.-|.....\../............-..|..|..................--..././
...................-......-...././............./.......\\...../|.................\..............\..|..........
.|.|.../..\|..-../........................................................-...................................
...............-...../.....|...|....\........|......|.............\..........................-...\............
..\.......................-..............\..-...........-...............\.\-....-.............................
..............-............/..-.......|....-/............../........|............../......|..................-
.......|..........|..-..../...................................|-......-..\......./........................\...
.........................................-......./..........|.....\../.../......|./.........................\\
...-.-.........../......./.-........//..|...................|.........-..........|........................-...
........./...../.........\.......\..............-........../-./..|.\...............|....................../...
........-.......\\.........-...\..//...-.........\\....../.-................\.......\...\............/...\....
................-...|......-.....-..|...../....../...-./.............-...........................-|...........
....-..-...........|.................../..\.....|......|...............-.-.|.........../.........../..|....../
....\......................................\................../..|...........-.\.....\.........||...........|.
.........................\.................-...............\.....\.-......................\.......-\.-........
...............-..\................\.................../.............-...................................../..
..-|../.-..|....\............-.......\.../....../...-...../|.........\..\..-..../.-..\....|........\..........
.........................../............|....................-......................................|.........
...../...........|.../........./............./......../.........-.............|..............-.......-|..-...|
..\/......./.\................/.......-............\..\............................................\..........
.......\.\.\......../|......\....................|..\..........-............./.........-...\....|../\......|..
...........\......././.........\......../...............................|........./...|.......................
............|.......|......./.............-..-.........\.....\....................../................./.......
...................................................../...../...............\........-.........................
.|...............-.............-........................|...............................-..|................/.
..............\....../../...........-.-..\........../...........-.....-......................|..../..|.|......
...................../...-.-........................../.........................|.....\.......................
.........\............\..............|.....//........-..\.../..................-.......//..........|......|..\
...../\.........\.......|....|....\...|..........\..............//........-..-.......-............-.-./.......
.........|.........................\...............................|/..........\..\...-..|..................\.
........................\..........|........./..-......\..../..|............\......./......................./|
................\.-...................|.......|/.-|....-.-.../......-.-...-.......-.........../.....-..|......
..........-.\......-.-....-............\..-.......\....-.../\..................|............/...|..../.....-..
|.....................|..\...................||............\...\......./........................-.....|..../..
.|..../.......|..........................................................|....../.....|.....|.....-...........
.......................|..-..\../.....|.........\.................-...........................................

View File

@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

204
2023/src/day16.f90 100644
View File

@ -0,0 +1,204 @@
program day16
implicit none
integer, parameter :: max_chars = 300
integer, parameter :: max_rows = 300
character(200) :: fname
character(max_chars) :: fline
character(max_chars) :: map(1:max_rows)
integer :: n_arguments
integer :: istat
integer :: rows, cols, strlen
logical :: energised(1:max_rows, 1:max_chars, 0:4)
integer :: i
integer :: n_en, sum_en
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)
rows = 0
do i=1,max_rows
read(10, "(a)", iostat=istat) fline
strlen = len_trim(fline)
if ((strlen .eq. 0) .or. (is_iostat_end(istat))) then
exit
end if
rows = rows + 1
map(rows) = fline
end do
close(10)
cols = len_trim(map(1))
do i=1,rows
print *, map(i)(1:cols)
end do
energised = .false.
call beam(1, 1, 0, 1, map, rows, cols)
n_en = n_energised(energised, rows, cols)
print *, "Number energised: ", n_en
sum_en = 0
do i=1,rows
energised = .false.
call beam(i, 1, 0, 1, map, rows, cols)
n_en = n_energised(energised, rows, cols)
if (n_en .gt. sum_en) then
sum_en = n_en
end if
energised = .false.
call beam(i, cols, 0, -1, map, rows, cols)
n_en = n_energised(energised, rows, cols)
if (n_en .gt. sum_en) then
sum_en = n_en
end if
end do
do i=1,cols
energised = .false.
call beam(1, i, 1, 0, map, rows, cols)
n_en = n_energised(energised, rows, cols)
if (n_en .gt. sum_en) then
sum_en = n_en
end if
energised = .false.
call beam(rows, i, -1, 0, map, rows, cols)
n_en = n_energised(energised, rows, cols)
if (n_en .gt. sum_en) then
sum_en = n_en
end if
end do
print *, "Max energised: ", sum_en
contains
recursive subroutine beam(s_y, s_x, s_yd, s_xd, map, rows, cols)
integer, intent(in) :: s_y, s_x, s_yd, s_xd
integer, intent(in) :: rows, cols
character(*), intent(in) :: map(:)
character(len=1) :: cell
integer, parameter :: max_iter = 500000
integer :: y, x, yd, xd
integer :: i
logical :: split
y = s_y
x = s_x
yd = s_yd
xd = s_xd
do i=1,max_iter
if ((x .lt. 1) .or. (x .gt. cols) .or. (y .lt. 1) .or. (y .gt. rows)) then
! Out of bounds
return
end if
if (energised(y, x, dir_i(yd, xd))) then
! In a loop
return
end if
! Mark where we've been
energised(y, x, 0) = .true.
energised(y, x, dir_i(yd, xd)) = .true.
! Work out where to go
cell = map(y)(x:x)
split = is_split(cell, yd, xd)
call cell_dir(cell, yd, xd)
if (split) then
! New beam in other direction
call beam(y-yd, x-xd, -yd, -xd, map, rows, cols)
end if
y = y + yd
x = x + xd
end do
print *, "Hit max iter"
stop
end subroutine beam
function is_split(cell, yd, xd)
character(len=1), intent(in) :: cell
integer, intent(in) :: yd, xd
logical :: is_split
if ((cell .eq. '-') .and. (yd .ne. 0)) then
is_split = .true.
else if ((cell .eq. '|') .and. (xd .ne. 0)) then
is_split = .true.
else
is_split = .false.
end if
end function is_split
subroutine cell_dir(cell, yd, xd)
character(len=1), intent(in) :: cell
integer, intent(inout) :: yd, xd
if (yd .eq. -1) then
if (cell .eq. '\' .or. cell .eq. '-') then
yd = 0
xd = -1
else if (cell .eq. '/') then
yd = 0
xd = 1
end if
else if (yd .eq. 1) then
if (cell .eq. '\' .or. cell .eq. '-') then
yd = 0
xd = 1
else if (cell .eq. '/') then
yd = 0
xd = -1
end if
else if (xd .eq. -1) then
if (cell .eq. '\' .or. cell .eq. '|') then
yd = -1
xd = 0
else if (cell .eq. '/') then
yd = 1
xd = 0
end if
else if (xd .eq. 1) then
if (cell .eq. '\' .or. cell .eq. '|') then
yd = 1
xd = 0
else if (cell .eq. '/') then
yd = -1
xd = 0
end if
else
print *, "Direction ????", yd, xd
stop
end if
end subroutine cell_dir
function dir_i(yd, xd)
implicit none
integer, intent(in) :: yd, xd
integer :: dir_i
if (yd .eq. -1) then
dir_i = 1
else if (xd .eq. 1) then
dir_i = 2
else if (yd .eq. 1) then
dir_i = 3
else if (xd .eq. -1) then
dir_i = 4
else
print *, "Direction ????", yd, xd
stop
end if
end function dir_i
function n_energised(en_map, rows, cols) result(n_en)
integer, intent(in) :: rows, cols
logical, intent(in) :: en_map(1:max_rows, 1:max_chars, 0:4)
integer :: n_en
integer :: i, i2
n_en = 0
do i=1,rows
do i2=1,cols
if (en_map(i, i2, 0)) then
n_en = n_en + 1
end if
end do
end do
end function n_energised
end program day16