···
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
17
+
function fix_and_get_middle_value(arr, left, right) result(res)
18
+
! we are going to fix the array swapping until its right
19
+
! there's probably a better way maybe? but I just want to get it done
21
+
integer, allocatable, intent(in) :: arr(:), left(:), right(:)
22
+
integer, allocatable :: work(:), copy(:)
23
+
integer :: res, i, j, tmp
25
+
allocate(copy(size(arr)))
29
+
outer: do i = 1, size(copy)
30
+
if(i+1 < size(copy)) then ! if we can check forward
31
+
if(allocated(work)) then
34
+
do j = 1, size(right) ! find value in right and save left to work
35
+
if (copy(i) == right(j)) then
36
+
if(.not. allocated(work)) then
40
+
call append_to_integer_array(work, left(j))
44
+
do j = i+1, size(copy)
45
+
if(any(work == copy(j))) then
55
+
if(.not. found) then ! if we are still valid lets keep going
56
+
if(allocated(work)) then
59
+
if(i-1 > 0) then ! if we can check backwards
60
+
do j = 1, size(left) ! find value in left and save left to work
62
+
if (copy(i) == left(j)) then
63
+
if(.not. allocated(work)) then
67
+
call append_to_integer_array(work, right(j))
71
+
do j = 1, size(copy)
72
+
if(any(work == copy(j) .and. j < i)) then
85
+
if(.not. found) exit
87
+
res = copy((size(copy) + 1)/2)
89
+
if(allocated(work)) then
92
+
end function fix_and_get_middle_value
19
-
integer :: io, ios, idx, a, b, ct, i, j, res
97
+
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)
183
+
res_part2 = res_part2 + fix_and_get_middle_value(print_list, left, right)
···
192
+
print*, "Part 2 Result ", res_part2