2023 Day 14

Note off-by-one for cycle numbering in part 2
main
Petra 2023-12-14 21:34:26 +13:00
parent d9e70ccca6
commit 3dd7a42797
5 changed files with 398 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
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
all: aoc19

101
2023/data/day14.txt 100644
View File

@ -0,0 +1,101 @@
O...O..###.#..#O...###.......#....O#.O...O....#.O...O..O..##.O.O#...O........O...#.#.O...O#O.......#
...O#..#O...#....O..###.O.....#......#.OO.O#..#.O..##...#....##...#.O....##O##OOO..#.O..OOOO..#..#.#
...##.OOOOO#.OO....OO#O.###.O..O...OO.......O....O...OO##....#..O.....#O#.O.O.#O..O.##....##.#.OOOO#
O..#O..O.....#.#...#....#O.O..#..O.O..#O#..O....O..O.#.O.#..O.O#...#O#...##O.#O.##.OO...#.#.#OOO....
.#O#O.O.......#O.#.O#.O..##.#.O.....#..#.....O..#..###..OO.#.O.OO#..O.O.........O..O...#.O.#....OO#.
..........#...O.OO....#.O.O##OO#......#OO.#.O...O....O..#OO#..O...#..#.#.....O#O#..OO#...#OOO.O.#O..
#O#OO.O.#.O.O...O.###O.........#O.O.#...O.#..O.O..#..O##.#O.O.OO.....OO.#.#.#..#.O..O#......O..O....
.#....O#O.OO..#..O#..O..O..O.#..O..#.O#....#.O....O.O.....#O#....#.#.#..O...O#OO#.#O.#O.#...OO..OO#O
.O.........#.#O#....O.O#OOO......O..#O#...#.....#.......#...#....#.##.O...O..#O..#O...O.#..O.OO..#..
O...O...O.....O..O...........OO.##..OO.OO#.O.##..O#OO#......O..O#.O#.OO..#####......O....#....O...#.
.....#O.....#....#O......#..OO.O.O.#..O..O....O..#.#..#.......O..O.O....O.OO......OOO#.OOO......O...
..O#.O.O.#O#O...#..........#..O##...O..O#OOO.OO..##O.#OOOO...#..O....#.....O...#....#.#.#...OO.O.#..
#.......#.O....O#.#O..#OOO..O..###O#..#.O...OO.O.......##O#..O#...OO.........OOO.#O...O...#OO#O#...O
#O#...#O.##..O.........O#..O..O..........#.##..OOOO...O..#.OO......O#....##....O.O...O.OO...O..OOO.O
.OO..#.....O#O..O..OOOO.#.#..#.O......#..#..O..O..O.#O..OO....#...O##O.O.O.O.#......O...O..#.#O...#.
..OO.O......O.O.OO.....O.#..#.O..#O.#.O.....#.O..OO.OOO#..#.#OO.#.OO#.....##O....#.#O.....#..#......
.#..O#.#O#.....##.O.#......O#.O.#..#O.#..O#.OO.O.#.O#..O.....#.##..###O..O.OO.##.O.O..#......###...O
#.O.O....O##...#..OO..#...O#O....O....O.OO.........#..OO..#...#...OOO#O.....O.#.OO.OO##.O....#......
O#.O.O..#..#....O.O.......#.#...#...##O..O.O...O.OO#..#O.O..O.......#...#O.#.O.#O##.#.O..#..O....#.#
.O.O..#...O#..O....O..#.#.OO......O.O.O.O##........#.O#.#..#....O..O....O...O##OOO..O.#O..O.......##
......#..........O#..O..O..O#.OOO#O.#........#...O.#..OO..#.#.#..O#...###.#...#.#.O#..#O.#O....O..O.
..#.......O...#..O..O#..........#.......#....#...O........##O....#O..OO.###.O#.#O........OO#.#O..O#O
.....#.O.O........O.#.O.#.O...##.......#..#........#.O..O...###...O.O.O#....OO.#.....#.......O#..O..
#..O.O.O#.....#.OOO##.....O..OOO.OO...#..#O..OO.O...##...#..###......O...##..#....#..OOO.O...#OO.#..
..##...O...#O..O#.O.#...O.O#......OO..O...O.#...O.O.OO...O.O.O.#..#O.O........#.#..##.O.....#.#O...O
..#O...O.O.#.O....O#.....#..O.O................O....O..##O#.#O....O.O......O...#.....O..O#...O..#.O.
..#..O.#......O#.O.O...O#...O#.O#...O..O....#..O...#.#......#......#O....O.O.O..O..O..#....O..O.....
..#...O....OOO.O##..O#.#.......O##.....O........OO.O...OO#.....#......O.#O#OO##O....O#.OO........#..
....O##..#.....##.O##...O.#..O...#O.#...#.#...#O....O.O.....O.OO.##..#O...#O.O#.#..O#...............
O..O.......O#O.......OO....O.OO....#...#O.OO#..##O....#..#.#O#OO##O...O.O...#...OO#.....O..#..OO....
..#..#O....#...O.##...#O#.O..O.O....O.....OO#O.#..#.....O..O.###.OO.O.......O..#.......OO.#.#..O....
#.O..O.O.#O..OOO##OO....O.OO#OO...#OO.#.#O.OO.OO...#..O.....O....O.O.O#.OO..O.#..#.O.O..OOO##..O#OO.
O.#O.#.OO.#...#.##..OO...#.O##..#O.#.#.#.....O.O##..O..#..O.O.....#....O...O......O..O##.###..#O.#OO
#.........#....O...O........OOOO.##.O.O.#......O####O..#..O##.O.#O.#O...#..#.....O...#.#..O#.#...#O.
.......#.#OOO.....O#O..#.#....#.OO.....O..OOOO#...#.............###.........O.OO..#.O#O..O...O..#.O.
O#...O.......O.O..O.#.O.....OO...O...O.O#OO....##.##.#O#..........O.O.......#.....O.....O.O..OOO#..#
#.O#......O.O..#O.O..O....#..#O..#.#O.O...OO...##.#.O.O..#..#O.#.#.O#.......O..O.#...#O.....#.O#...O
..O..O#...##.#..##..#O..O.OO#..#.##O.O..O..#.O..##.O#.O..#.O.#...........O#.O...O.O#.#...#..#..O.O.O
#.#.O...OO....O..O.#O.#.OO..O#..O..O....O.#.#O.##O.....#.O.#...#...#....#....OO.......#.O..O...#..#.
..O.O#O..O.......#.......O#OO..O....O##.....O.#.O..#.#.....#...OOO.......#...OO#......O..........O.O
..##.#..O.O..#....##..O...O..O#.#O....O##....##O.....OO.O..#.O.O#.#.....OO..O........#........O#O###
..#.#...O#..O.......#.OO##...OO#O...#.O....#OOO......O.O.O.#O..#.....##..#O#.OOO...O......OOO#.O.O.O
O...#...O..#O......O.O......#.....#...#..#.O...O.#O##..#.#...#..O#....O.#OO#..O.....#.#..#OOO#...OOO
...##..#O...#...O.##O.O#.........O........OO#O..#.OO.#....O..#...OO..#O.......O..O.O#...#......OO..#
.....O.O....O.O#....#O#......O.............O.OO#.....#OO...........O#.......#......OO#O#.OO..O..O...
O##.....O...#.#.#..O..OOOOO..#.#.O##.O...#..O...O.....O.#.#...###O.O..#.#.O..O#....#.O...O#.O.......
#OO.....O.O.#...#.O#.....OO...##.O...O......O.#O.........#.O....OO.OO.#.O.#O.O......#O.###..#.#.OO#.
.OOO..O.OO#O..O##.......##...##......O..O...#...O...#.O.O.#.#O.#OO...O.O..#.O#...O..#...OO....#OO...
.##.O.OOO..O#.##O...#O#O...#...#.....#.....O...O......#..##O.OOO.O#..O.#..#.#..O........O..#.O#O....
....O..O.............#......O.#O..OO...OO.....O..#..#......OO......O....OO..OOOO.....#.#O..#.#...O.O
.###.....##.#......###....O#....O....O...O....#.#O.O#..#..O..#O...OO..OO.O.....#O.O..#.#.....O...O#.
..O..#O...O.OO.O........O...O.O.....O..#O.O.O...#........O........OO.O..OO#O.#.O..#.#O#...O.#.##.O..
OO..OO.O..#OO..OO#..........#...............#.O#....O...O.#.O.O#..#.#.OO#...O.#O..##........O#.O....
#.O......#....#...O.O...O....OO..O..#...O..O..#..O#........###...#..#.#..OO..O.....#.....#...#...O..
..#O.O..O.....OO...............O....#.....O.....#O.OO.O..O..##O....O..#.#.#O...O#O##O#.#OOOOOO.#..O.
#O#O.....O..#...O#......OO...#..OOO#O..O##.OO..#.#.##.#.OO..O.###.O.#..O...#O#....O....O##O..#O.O.O.
..O.....O#.O#.....OOO.O..O......O.O..#O..O.O.........#.###..#.O.O.O.OO#.O..#...O.#..#.#...O##.#...O.
#..O#..O#..#...#.#......##.....#OO.O#.OO..#.....OO#...O.O..O.......O#.#.........O.#O#O.O..#.O.......
..O.#..#..O.O..##..#.#O.......OOO#.O.#.OO.##...O...........#.O..#...#.O........O..O..#.....O.#.....O
O.....O.....O...O....#..#......OO#....O......O..O#.#...O...##.O####O#..O.OO.O.O.O.##...........O...O
OO#.O..##O.O..O..#.#..#...#...O.O.#..#...#.#...O.#..#O......#####..##.OOOOO......O.O..#OO.O.#..O.O..
O.O..#.O.OO.#.O.#....O.#......O.##..#O...O..##.#...O.#...O.O....#...O.#...O.......###....#....#.....
#..#.........O....#........OO.#..OO..OOO....#.....O....##O.O.O....O.O..O....#O#.###.O...#..O.O....#O
#...O##..O#..O..#.O.......O.....#....#O###O#OO.O.O..O...O..OO...#.#.#..O..O...O..#...#.#....#.#.#.OO
......#..........#...#..O..#O#..O....#.O..O..#.O.#O...O#.......#........O#O....OO.....O..OO...#..#..
O..O...#...#..O##....#.O....O..#...O..###..O...O....OO..O..O.........O..##OO......#O...O.O##...OOO.O
..#......OO.#.......#...O#.#.O#...#...#O...O......#.....O#....OO........O#O.......#O..#OO#.O.O....#.
.O.##....#OO.O...O#....O..O......O..#.....#O..O.#...#.......O....OO#...O....#....#.O...OOOOO.#O.O..O
....#....OO#..#O....O...#.OO#...#O......#.#.OOO..#...O#......#...O..##.OO..O....O.#O...OO.#.#.O..O..
O.O....OO#...#..O...O.#OO.O..O.....#....#.O..O.#..........OO..#.#.#.O...O.O.#.#..#.#......#..O.###..
....#..O.O....OO...O.....#.......#........O..#.OO##O#OOO#O.......#.#.#....#.#.O..#.O##.#..O....##.OO
O.....#.#....#.........O.O....O..O#.....#O.O#O...O#O...O...##........OO.O.........OO..#...OO.O..#...
.O....O....#.#O..O...OOO.......O..O..O..#............O.....#.O...OO....#.O#O.O##.#.O........#...O...
..##.O..OO#..O.O...O....##...O.......#..O...O.#O.O..........O.#.#OOO#..O...O..O.OOO.....O.........O.
OO..#..#.....#....##...#..#..##.O.OO...O#........O..O.....#.......O.O.O..O...O.O.OO..O...#.###.#.O##
O.#O...O......O.......O#.O......O#..O..#...##.O...##......O.O.#O.##.O.O#....O#O#..#..#O#...#........
#OOO.O...O...O.#.#O....#....O..#.#..O.......#OO......O........O#..O..##..#.O...O...##O...........O#O
.O.#.OO..O.....O...##O.#O.#...O#..O.#....#..#OO#.#.OO...#.O#...OO......O.....#O..O...O...OOO#.O..O##
..O...O...#..O..O#..O.OO.#.OO.O...OO.#.....#.O..#..#O..OOO.##O#O....#...#O.O....OO..#.O...OO#O#....O
#O..OO.###.OOOO....O#......O.O.#OO......OO..O.O..O...O...O..#.OO.....##.O.OO...O..#O.###..O..OO.#.O.
...O.O##....O.#..O#.OOO#.........#....#....#.......#.O...O...........O.O#O.O#..OO.O#....O.#.O.#O.O..
#....#....O..O..#.....#..O#O.#..O.O.#O.O....OO.#.#.O.O....#O....#..O..#.O.#O...#.....O.O...#..#...O.
.#....O.....O..OO.O.O#..#...##.....OO....#.#.OO.#..O..O.......#......#.....OOO#O.#######.##......O#O
#OO..#..OOOO.O..#.###..#.O..#.O...O#.O..#....#...#OO#..#....#OO....#...OO..#...O.O.....OO...#.O.#...
#..O....OO....#.#.OO...O.O....O.##..#.O..O....OO..#.O..O.OO......#.#O..O.O........O.O.O...O.....O#.O
.O.#...O.#....O..O#.O..O.OO.....O...........#.##OO....O..#O###..#..O.O.O.....#.......##O#.#...O.....
.....O.OO.#O.O##............O.O...##.O......#....#....#...#.#...O...#OO.#.O.....O.#..O...........#..
..O.OO..#O..O.O.##.O#..........OO..O...OO..OO.OO.OO#...O.#O.O...OO.......#OO.#..#..O...#....O.......
O.OO....O#....#...#.#O##..O#...#..#......O.......#O..#O..O..O...#......#O..O##.O....O..O.O#.#....O..
.#.O..O..O.........##O..#..OOO...O##.O.#O#.O...O.#....#..O..O.......O..#..O..O#..#....O..OO.##..O...
...#...OO.OOO..#...O.#.O.......#...##.O#.##..##..#..#.##.....##O..O..O..#OO...#....O..O.......#..#O.
.....O...O.#......##O.O..#...#.....O..O....#O#.#..#O.#....O#.O...#O....###...OOO...O#..O..O..###..O.
..#..........O..O..O...O...O....O............O.....#O....O..#.....#...O.....#..O.#.#..O..O...O.#O...
...OOO......O...#.O#....##..#...O..O#..##..O....#.O.OO.......#....O....##.#.#.O...O#.#O..OOO..O.O...
O...#.O....O..OO..O...O....#.#.....#O#O....#O.##.O.OO.#.O............#.O#...OO..OO#..#O.#....O......
#.O.....#.O#.OO.#........O..O.#.OO.........#..#...#.O#O...O...O..OO.#.......O.O..#..O.O...OO..O..O.O
..#....#O#.O.O#.OO..O...#..O......O#O.......OO..OO.....#O#.O.O..#O.......OO.#.O..O....O.O.O.........
#...O###O..O.O##..#.#.##..#...#.#.#...O.#....#.....#.#...O#..O.O...O..O........#.###....O...#..#...#
.......O........O.....##..O##...O...OO..#O..O...#...O.........#OO.....OO.O..O..#OO.#.#O..O#.OO...#O.
.OO..O.....#.O.....#O..#O...O....#..O.....#....#O......O...#.O###.OO......O..O.......#####.........O

View File

@ -0,0 +1,10 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....

74
2023/src/day14.f90 100644
View File

@ -0,0 +1,74 @@
program day14
implicit none
integer, parameter :: max_chars = 300
integer, parameter :: max_rows = 300
character(200) :: fname
character(max_chars) :: fline
integer :: n_arguments
integer :: istat
character(len=max_chars) :: map(1:max_rows)
character(len=1) :: cchar
integer :: rows, cols, strlen
integer :: i, i2, i3, tot_weight, tweight
logical :: rest
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
cols = len_trim(map(1))
tot_weight = 0
print *, "Starting position:"
do i3=1,rows
print *, trim(map(i3))
end do
print *
do i=1,rows
do i2=1,cols
if (map(i)(i2:i2) .eq. 'O') then
map(i)(i2:i2) = '.'
rest = .false.
if (i .gt. 1) then
do i3=i-1,1,-1
cchar = map(i3)(i2:i2)
if (cchar .ne. '.') then
map(i3+1)(i2:i2) = 'O'
tweight = rows - i3
rest = .true.
exit
end if
end do
end if
if (.not. rest) then
map(1)(i2:i2) = 'O'
tweight = rows
end if
tot_weight = tot_weight + tweight
end if
end do
end do
print *, "End position:"
do i3=1,rows
print *, trim(map(i3))
end do
print *
print *, "Total weighting: ", tot_weight
end program day14

212
2023/src/day14b.f90 100644
View File

@ -0,0 +1,212 @@
program day14
implicit none
integer, parameter :: max_chars = 300
integer, parameter :: max_rows = 300
integer, parameter :: max_cycles = 200
integer, parameter :: cycle_est = 1000000001
character(200) :: fname
character(max_chars) :: fline
integer :: n_arguments
integer :: istat
character(len=max_chars) :: map(1:max_rows), map_prev(1:max_rows)
character(len=max_chars) :: metamap(1:max_cycles, 1:max_rows)
integer :: rows, cols, strlen
integer :: i, i2, cycles
logical :: eq, f_match
integer :: match_c1, match_c2, cycle_eqv, weight
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
cols = len_trim(map(1))
metamap(1,:) = map
f_match = .false.
do cycles=1,max_cycles-1
map_prev = metamap(cycles, :)
call cyce(map_prev, map, rows, cols)
metamap(cycles+1, :) = map
do i2=1, cycles
map_prev = metamap(i2, :)
eq = all_eq(metamap(i2, :), map, rows)
if (eq) then
if (.not. f_match) then
f_match = .true.
match_c1 = i2
match_c2 = cycles+1
end if
print *, "Match: ", i2, cycles+1
exit
end if
end do
if (f_match) then
exit
end if
end do
print *
if (f_match) then
print *, "Found match! ", match_c1, match_c2
print *, "Cycle length: ", match_c2 - match_c1
cycle_eqv = mod(cycle_est - match_c1, match_c2 - match_c1) + match_c1
print *, "Cycle equivalency: ", cycle_est, cycle_eqv
weight = cal_weight(metamap(cycle_eqv, :), rows, cols)
print *, "Weight: ", weight
else
print *, "Did not find match after ", max_cycles, " cycles"
end if
contains
subroutine cyce(initial, cycled, rows, cols)
implicit none
character(len=max_chars), intent(in) :: initial(:)
character(len=max_chars), intent(out) :: cycled(:)
integer, intent(in) :: rows, cols
integer :: i, i2, i3
logical :: rest
character(len=1) :: cchar
! Copy
do i=1,rows
cycled(i) = initial(i)
end do
! Roll North
do i=1,rows
do i2=1,cols
if (map(i)(i2:i2) .eq. 'O') then
map(i)(i2:i2) = '.'
rest = .false.
if (i .gt. 1) then
do i3=i-1,1,-1
cchar = map(i3)(i2:i2)
if (cchar .ne. '.') then
map(i3+1)(i2:i2) = 'O'
rest = .true.
exit
end if
end do
end if
if (.not. rest) then
map(1)(i2:i2) = 'O'
end if
end if
end do
end do
! Roll West
do i2=1,cols
do i=1,rows
if (map(i)(i2:i2) .eq. 'O') then
map(i)(i2:i2) = '.'
rest = .false.
if (i2 .gt. 1) then
do i3=i2-1,1,-1
cchar = map(i)(i3:i3)
if (cchar .ne. '.') then
map(i)(i3+1:i3+1) = 'O'
rest = .true.
exit
end if
end do
end if
if (.not. rest) then
map(i)(1:1) = 'O'
end if
end if
end do
end do
! Roll South
do i=rows,1,-1
do i2=1,cols
if (map(i)(i2:i2) .eq. 'O') then
map(i)(i2:i2) = '.'
rest = .false.
if (i .lt. rows) then
do i3=i+1,rows
cchar = map(i3)(i2:i2)
if (cchar .ne. '.') then
map(i3-1)(i2:i2) = 'O'
rest = .true.
exit
end if
end do
end if
if (.not. rest) then
map(rows)(i2:i2) = 'O'
end if
end if
end do
end do
! Roll East
do i2=cols, 1, -1
do i=1,rows
if (map(i)(i2:i2) .eq. 'O') then
map(i)(i2:i2) = '.'
rest = .false.
if (i2 .lt. cols) then
do i3=i2+1,cols
cchar = map(i)(i3:i3)
if (cchar .ne. '.') then
map(i)(i3-1:i3-1) = 'O'
rest = .true.
exit
end if
end do
end if
if (.not. rest) then
map(i)(cols:cols) = 'O'
end if
end if
end do
end do
end subroutine cyce
function all_eq(initial, cycled, rows)
implicit none
character(len=max_chars), intent(in) :: initial(:), cycled(:)
integer, intent(in) :: rows
integer :: i
logical :: all_eq
all_eq = .true.
do i=1,rows
if (trim(initial(i)) .ne. trim(cycled(i))) then
all_eq = .false.
!print *, trim(initial(i))
!print *, trim(cycled(i))
return
end if
end do
end function all_eq
function cal_weight(map, rows, cols)
implicit none
character(len=max_chars), intent(in) :: map(:)
integer, intent(in) :: rows, cols
integer :: cal_weight
integer :: i, i2
cal_weight = 0
do i=1,rows
do i2=1,cols
if (map(i)(i2:i2) .eq. 'O') then
cal_weight = cal_weight + (rows - i) + 1
end if
end do
end do
end function cal_weight
end program day14