My yearly advent-of-code solutions
1module day_08_utils 2 implicit none 3 type node 4 character :: c 5 integer, allocatable :: x(:), y(:) 6 end type node 7contains 8 subroutine append_to_integer_array(arr, val) 9 implicit none 10 integer, allocatable, intent(inout) :: arr(:) 11 integer, intent(in) :: val 12 integer, allocatable :: temp(:) 13 if(.not. allocated(arr)) then 14 ERROR STOP 'Array not allocated' 15 end if 16 allocate(temp(size(arr) + 1)) 17 temp(1:size(arr)) = arr 18 temp(size(arr) + 1) = val 19 call move_alloc(temp, arr) 20 end subroutine append_to_integer_array 21 22 subroutine add_node(nodes, c, x, y) 23 type(node), allocatable, intent(inout) :: nodes(:) 24 type(node), allocatable :: temp(:) 25 character, intent(in) :: c 26 integer, intent(in) :: x, y 27 integer :: i 28 do i = 1, size(nodes) 29 if (nodes(i)%c == c) then 30 if(.not. allocated(nodes(i)%x)) then 31 allocate(nodes(i)%x(1)) 32 nodes(i)%x(1) = x 33 else 34 call append_to_integer_array(nodes(i)%x, x) 35 end if 36 if(.not. allocated(nodes(i)%y)) then 37 allocate(nodes(i)%y(1)) 38 nodes(i)%y(1) = y 39 else 40 call append_to_integer_array(nodes(i)%y, y) 41 end if 42 return 43 end if 44 end do 45 allocate(temp(size(nodes) + 1)) 46 temp(1:size(nodes)) = nodes 47 allocate(temp(size(nodes) + 1)%x(1)) 48 allocate(temp(size(nodes) + 1)%y(1)) 49 temp(size(nodes) + 1)%c = c 50 temp(size(nodes) + 1)%x(1) = x 51 temp(size(nodes) + 1)%y(1) = y 52 call move_alloc(temp, nodes) 53 end subroutine add_node 54end module day_08_utils 55 56program day_08 57 use day_08_utils 58 implicit none 59 character(len=50) :: lines(50) 60 integer :: io, i, j, k, l, score, xdist, ydist 61 integer, allocatable :: anti_x(:), anti_y(:) 62 type(node), allocatable :: nodes(:) 63 logical :: found 64 65 open(newunit=io, file='day_08_input.txt', status='old', action='read') 66 read(io, '(A)') lines 67 score = 0 68 do i = 1, 50 69 do j = 1, 50 70 if(lines(i)(j:j) /= '.') then 71 if(.not. allocated(nodes)) then 72 allocate(nodes(1)) 73 nodes(1)%c = lines(i)(j:j) 74 allocate(nodes(1)%x(1)) 75 allocate(nodes(1)%y(1)) 76 nodes(1)%x(1) = j 77 nodes(1)%y(1) = i 78 else 79 call add_node(nodes, lines(i)(j:j), j, i) 80 end if 81 end if 82 end do 83 end do 84 do i = 1, size(nodes) 85 do j = 1, size(nodes(i)%x) 86 do l = 1, size(nodes(i)%x) 87 if(nodes(i)%x(j) == nodes(i)%x(l) .and. nodes(i)%y(j) == nodes(i)%y(l)) cycle ! don't compare same values 88 xdist = nodes(i)%x(j) - nodes(i)%x(l) 89 ydist = nodes(i)%y(j) - nodes(i)%y(l) 90 if(nodes(i)%x(j) + xdist > 0 .and. nodes(i)%x(j) + xdist <= 50 .and. & 91 nodes(i)%y(j) + ydist > 0 .and. nodes(i)%y(j) + ydist <= 50) then 92 if(.not. allocated(anti_x)) then 93 allocate(anti_x(1)) 94 anti_x(1) = nodes(i)%x(j) + xdist 95 allocate(anti_y(1)) 96 anti_y(1) = nodes(i)%y(j) + ydist 97 score = score + 1 98 else 99 found = .false. 100 do k = 1, size(anti_x) 101 if(anti_x(k) == nodes(i)%x(j) + xdist .and. anti_y(k) == nodes(i)%y(j) + ydist) then 102 found = .true. 103 exit 104 end if 105 end do 106 if(.not. found) then 107 call append_to_integer_array(anti_x, nodes(i)%x(j) + xdist) 108 call append_to_integer_array(anti_y, nodes(i)%y(j) + ydist) 109 110 score = score + 1 111 end if 112 end if 113 end if 114 if(nodes(i)%x(l) - xdist > 0 .and. nodes(i)%x(l) - xdist <= 50 .and. & 115 nodes(i)%y(l) - ydist > 0 .and. nodes(i)%y(l) - ydist <= 50) then 116 if(.not. allocated(anti_x)) then 117 allocate(anti_x(1)) 118 anti_x(1) = nodes(i)%x(l) - xdist 119 allocate(anti_y(1)) 120 anti_y(1) = nodes(i)%y(l) - ydist 121 score = score + 1 122 else 123 found = .false. 124 do k = 1, size(anti_x) 125 if(anti_x(k) == nodes(i)%x(l) - xdist .and. anti_y(k) == nodes(i)%y(l) - ydist) then 126 found = .true. 127 exit 128 end if 129 end do 130 if(.not. found) then 131 call append_to_integer_array(anti_x, nodes(i)%x(l) - xdist) 132 call append_to_integer_array(anti_y, nodes(i)%y(l) - ydist) 133 134 score = score + 1 135 end if 136 end if 137 end if 138 end do 139 end do 140 end do 141 print*, "Total : ", score 142end program day_08