2023 Day 13
parent
3477b81b21
commit
d9e70ccca6
|
@ -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
|
||||
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
|
||||
|
||||
all: aoc19
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,15 @@
|
|||
#.##..##.
|
||||
..#.##.#.
|
||||
##......#
|
||||
##......#
|
||||
..#.##.#.
|
||||
..##..##.
|
||||
#.#.##.#.
|
||||
|
||||
#...##..#
|
||||
#....#..#
|
||||
..##..###
|
||||
#####.##.
|
||||
#####.##.
|
||||
..##..###
|
||||
#....#..#
|
|
@ -0,0 +1,185 @@
|
|||
program day13
|
||||
implicit none
|
||||
integer, parameter :: max_chars = 300
|
||||
integer, parameter :: max_rows = 300
|
||||
integer, parameter :: max_iter = 2000
|
||||
character(200) :: fname
|
||||
character(max_chars) :: fline
|
||||
character(max_chars) :: pattern(1:max_rows)
|
||||
integer :: n_arguments
|
||||
integer :: istat, strlen
|
||||
integer :: patheight
|
||||
integer :: part1tot, p2tot
|
||||
integer :: i
|
||||
|
||||
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)
|
||||
patheight = 0
|
||||
part1tot = 0
|
||||
p2tot = 0
|
||||
do i=1,max_iter
|
||||
read(10, "(a)", iostat=istat) fline
|
||||
strlen = len_trim(fline)
|
||||
if ((strlen .eq. 0) .or. (is_iostat_end(istat))) then
|
||||
! Reached end of pattern
|
||||
if (patheight .gt. 0) then
|
||||
part1tot = part1tot + eval_pattern(pattern, patheight)
|
||||
p2tot = p2tot + eval_pattern_smudge(pattern, patheight)
|
||||
end if
|
||||
patheight = 0
|
||||
else
|
||||
patheight = patheight + 1
|
||||
pattern(patheight) = fline
|
||||
end if
|
||||
if (is_iostat_end(istat)) then
|
||||
! Reached end of file
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
close(10)
|
||||
print *, "Summary value: ", part1tot
|
||||
print *, "Summary value 2: ", p2tot
|
||||
|
||||
contains
|
||||
function eval_pattern(pattern, height) result(sumval)
|
||||
implicit none
|
||||
character(*), intent(in) :: pattern(:)
|
||||
integer, intent(in) :: height
|
||||
integer :: sumval
|
||||
integer :: width
|
||||
integer :: i, i2, mi2, i3
|
||||
logical :: same
|
||||
sumval = 0
|
||||
width = len_trim(pattern(1))
|
||||
print *
|
||||
print *, ' 123456789'
|
||||
do i=1, height
|
||||
write(*, 20) i, trim(pattern(i))
|
||||
end do
|
||||
do i=1,width-1
|
||||
same = .true.
|
||||
do i2=1,i
|
||||
mi2 = (i-i2) + i + 1
|
||||
if (mi2 .gt. width) then
|
||||
continue
|
||||
else
|
||||
do i3=1,height
|
||||
if (pattern(i3)(i2:i2) .ne. pattern(i3)(mi2:mi2)) then
|
||||
same = .false.
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
end if
|
||||
if (.not. same) then
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
if (same) then
|
||||
write(*, 21) i
|
||||
sumval = i
|
||||
return
|
||||
end if
|
||||
end do
|
||||
do i=1,height-1
|
||||
same = .true.
|
||||
do i2=1,i
|
||||
mi2 = (i-i2) + i + 1
|
||||
if (mi2 .gt. height) then
|
||||
continue
|
||||
else
|
||||
do i3=1,width
|
||||
if (pattern(i2)(i3:i3) .ne. pattern(mi2)(i3:i3)) then
|
||||
same = .false.
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
end if
|
||||
if (.not. same) then
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
if (same) then
|
||||
sumval = i * 100
|
||||
write(*, 22) i
|
||||
return
|
||||
end if
|
||||
end do
|
||||
print *, "???"
|
||||
20 format(i2, ' ', a)
|
||||
21 format("Vertical reflection at ", i2)
|
||||
22 format("Horizontal reflection at ", i2)
|
||||
end function eval_pattern
|
||||
|
||||
function eval_pattern_smudge(pattern, height) result(sumval)
|
||||
implicit none
|
||||
character(*), intent(in) :: pattern(:)
|
||||
integer, intent(in) :: height
|
||||
integer :: sumval
|
||||
integer :: width
|
||||
integer :: i, i2, mi2, i3
|
||||
integer :: devi
|
||||
sumval = 0
|
||||
width = len_trim(pattern(1))
|
||||
do i=1,width-1
|
||||
devi = 0
|
||||
do i2=1,i
|
||||
mi2 = (i-i2) + i + 1
|
||||
if (mi2 .gt. width) then
|
||||
continue
|
||||
else
|
||||
do i3=1,height
|
||||
if (pattern(i3)(i2:i2) .ne. pattern(i3)(mi2:mi2)) then
|
||||
devi = devi + 1
|
||||
end if
|
||||
if (devi .gt. 1) then
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
end if
|
||||
if (devi .gt. 1) then
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
if (devi .eq. 1) then
|
||||
sumval = i
|
||||
return
|
||||
end if
|
||||
end do
|
||||
do i=1,height-1
|
||||
devi = 0
|
||||
do i2=1,i
|
||||
mi2 = (i-i2) + i + 1
|
||||
if (mi2 .gt. height) then
|
||||
continue
|
||||
else
|
||||
do i3=1,width
|
||||
if (pattern(i2)(i3:i3) .ne. pattern(mi2)(i3:i3)) then
|
||||
devi = devi + 1
|
||||
end if
|
||||
if (devi .gt. 1) then
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
end if
|
||||
if (devi .gt. 1) then
|
||||
exit
|
||||
end if
|
||||
end do
|
||||
if (devi .eq. 1) then
|
||||
sumval = i * 100
|
||||
return
|
||||
end if
|
||||
end do
|
||||
print *, "???"
|
||||
end function eval_pattern_smudge
|
||||
|
||||
end program day13
|
||||
|
Loading…
Reference in New Issue