···
+
integer, allocatable :: x(:), y(:)
+
subroutine append_to_integer_array(arr, val)
+
integer, allocatable, intent(inout) :: arr(:)
+
integer, intent(in) :: val
+
integer, allocatable :: temp(:)
+
if(.not. allocated(arr)) then
+
ERROR STOP 'Array not allocated'
+
allocate(temp(size(arr) + 1))
+
temp(1:size(arr)) = arr
+
temp(size(arr) + 1) = val
+
call move_alloc(temp, arr)
+
end subroutine append_to_integer_array
+
subroutine add_node(nodes, c, x, y)
+
type(node), allocatable, intent(inout) :: nodes(:)
+
type(node), allocatable :: temp(:)
+
character, intent(in) :: c
+
integer, intent(in) :: x, y
+
if (nodes(i)%c == c) then
+
if(.not. allocated(nodes(i)%x)) then
+
allocate(nodes(i)%x(1))
+
call append_to_integer_array(nodes(i)%x, x)
+
if(.not. allocated(nodes(i)%y)) then
+
allocate(nodes(i)%y(1))
+
call append_to_integer_array(nodes(i)%y, y)
+
allocate(temp(size(nodes) + 1))
+
temp(1:size(nodes)) = nodes
+
allocate(temp(size(nodes) + 1)%x(1))
+
allocate(temp(size(nodes) + 1)%y(1))
+
temp(size(nodes) + 1)%c = c
+
temp(size(nodes) + 1)%x(1) = x
+
temp(size(nodes) + 1)%y(1) = y
+
call move_alloc(temp, nodes)
+
end subroutine add_node
+
end module day_08_utils
+
character(len=50) :: lines(50)
+
integer :: io, i, j, k, l, score, xdist, ydist, next_x, next_y
+
integer, allocatable :: anti_x(:), anti_y(:)
+
type(node), allocatable :: nodes(:)
+
open(newunit=io, file='day_08_input.txt', status='old', action='read')
+
if(lines(i)(j:j) /= '.') then
+
if(.not. allocated(nodes)) then
+
nodes(1)%c = lines(i)(j:j)
+
allocate(nodes(1)%x(1))
+
allocate(nodes(1)%y(1))
+
call add_node(nodes, lines(i)(j:j), j, i)
+
do j = 1, size(nodes(i)%x)
+
if(.not. allocated(anti_x)) then
+
anti_x(1) = nodes(i)%x(j)
+
anti_y(1) = nodes(i)%y(j)
+
if(anti_x(k) == nodes(i)%x(j) .and. anti_y(k) == nodes(i)%y(j)) then
+
call append_to_integer_array(anti_x, nodes(i)%x(j))
+
call append_to_integer_array(anti_y, nodes(i)%y(j))
+
do l = 1, size(nodes(i)%x)
+
if(nodes(i)%x(j) == nodes(i)%x(l) .and. nodes(i)%y(j) == nodes(i)%y(l)) cycle ! don't compare same values
+
xdist = nodes(i)%x(j) - nodes(i)%x(l)
+
ydist = nodes(i)%y(j) - nodes(i)%y(l)
+
next_x = nodes(i)%x(j) + xdist
+
next_y = nodes(i)%y(j) + ydist
+
if(next_x> 0 .and. next_x <= 50 .and. &
+
next_y > 0 .and. next_y <= 50) then
+
if(anti_x(k) == next_x .and. anti_y(k) == next_y) then
+
call append_to_integer_array(anti_x, next_x)
+
call append_to_integer_array(anti_y, next_y)
+
next_x = next_x + xdist
+
next_y = next_y + ydist
+
next_x = nodes(i)%x(l) - xdist
+
next_y = nodes(i)%y(l) - ydist
+
if(next_x > 0 .and. next_x <= 50 .and. &
+
next_y > 0 .and. next_y <= 50) then
+
if(anti_x(k) == next_x .and. anti_y(k) == next_y) then
+
call append_to_integer_array(anti_x, next_x)
+
call append_to_integer_array(anti_y, next_y)
+
next_x = next_x - xdist
+
next_y = next_y - ydist
+
print*, "Total : ", score