diff --git a/2023/Makefile b/2023/Makefile index f5a7c6a..f198555 100644 --- a/2023/Makefile +++ b/2023/Makefile @@ -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 diff --git a/2023/data/day14.txt b/2023/data/day14.txt new file mode 100644 index 0000000..660b5a3 --- /dev/null +++ b/2023/data/day14.txt @@ -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 + diff --git a/2023/data/day14ex1.txt b/2023/data/day14ex1.txt new file mode 100644 index 0000000..5a24dce --- /dev/null +++ b/2023/data/day14ex1.txt @@ -0,0 +1,10 @@ +O....#.... +O.OO#....# +.....##... +OO.#O....O +.O.....O#. +O.#..O.#.# +..O..#O..O +.......O.. +#....###.. +#OO..#.... diff --git a/2023/src/day14.f90 b/2023/src/day14.f90 new file mode 100644 index 0000000..601e278 --- /dev/null +++ b/2023/src/day14.f90 @@ -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 + diff --git a/2023/src/day14b.f90 b/2023/src/day14b.f90 new file mode 100644 index 0000000..70c6a7d --- /dev/null +++ b/2023/src/day14b.f90 @@ -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 + +