···
subroutine append_to_integer_array(arr, val)
integer, allocatable, intent(inout) :: arr(:)
integer, intent(in) :: val
integer, allocatable :: temp(:)
···
temp(size(arr) + 1) = val
call move_alloc(temp, arr)
end subroutine append_to_integer_array
-
integer :: io, ios, idx, a, b, ct, i, j, res
integer, allocatable :: left(:), right(:), print_list(:), work(:)
character(len=100) ::line
open(newunit=io, file='./day_05_input.txt', status='old', action='read')
read(io, '(a)', iostat=ios) line
···
res = res + print_list((size(print_list) + 1)/2)
···
···
subroutine append_to_integer_array(arr, val)
integer, allocatable, intent(inout) :: arr(:)
integer, intent(in) :: val
integer, allocatable :: temp(:)
···
temp(size(arr) + 1) = val
call move_alloc(temp, arr)
end subroutine append_to_integer_array
+
function fix_and_get_middle_value(arr, left, right) result(res)
+
! we are going to fix the array swapping until its right
+
! there's probably a better way maybe? but I just want to get it done
+
integer, allocatable, intent(in) :: arr(:), left(:), right(:)
+
integer, allocatable :: work(:), copy(:)
+
integer :: res, i, j, tmp
+
allocate(copy(size(arr)))
+
outer: do i = 1, size(copy)
+
if(i+1 < size(copy)) then ! if we can check forward
+
if(allocated(work)) then
+
do j = 1, size(right) ! find value in right and save left to work
+
if (copy(i) == right(j)) then
+
if(.not. allocated(work)) then
+
call append_to_integer_array(work, left(j))
+
if(any(work == copy(j))) then
+
if(.not. found) then ! if we are still valid lets keep going
+
if(allocated(work)) then
+
if(i-1 > 0) then ! if we can check backwards
+
do j = 1, size(left) ! find value in left and save left to work
+
if (copy(i) == left(j)) then
+
if(.not. allocated(work)) then
+
call append_to_integer_array(work, right(j))
+
if(any(work == copy(j) .and. j < i)) then
+
res = copy((size(copy) + 1)/2)
+
if(allocated(work)) then
+
end function fix_and_get_middle_value
+
integer :: io, ios, idx, a, b, ct, i, j, res, res_part2
integer, allocatable :: left(:), right(:), print_list(:), work(:)
character(len=100) ::line
open(newunit=io, file='./day_05_input.txt', status='old', action='read')
read(io, '(a)', iostat=ios) line
···
res = res + print_list((size(print_list) + 1)/2)
+
res_part2 = res_part2 + fix_and_get_middle_value(print_list, left, right)
···
+
print*, "Part 2 Result ", res_part2