My yearly advent-of-code solutions
1module sorting 2 ! apparently you need to put the subroutine in a module 3 implicit none 4contains 5 subroutine sort(arr) 6 !! a little insertion sort KISS 7 !! also yea we have no in built sorting 8 implicit none 9 integer, intent(inout) :: arr(1000) 10 integer :: i, j, key 11 do i = 2, 1000 12 key = arr(i) 13 j = i - 1 14 do while (j > 0 .and. arr(j) > key) 15 arr(j+1) = arr(j) 16 j = j - 1 17 end do 18 arr(j+1) = key 19 end do 20 end subroutine sort 21end module sorting 22 23program day_01 24 use sorting 25 implicit none 26 integer :: io, i, dist, sim, left(1000), right(1000) ! yea we are coding to the input file and not generic 27 open(newunit=io, file='./day_01_input.txt', status='old', action='read') 28 29 do i = 1, 1000 30 read(io, *) left(i), right(i) ! read both values in line 31 end do 32 33 close(10) ! close the file 34 35 call sort(left) 36 call sort(right) 37 38 ! if we dont init these bad things happen on multiple runs 39 dist = 0 40 sim = 0 41 42 do i = 1, 1000 43 dist = dist + abs(left(i) - right(i)) 44 sim = sim + left(i) * count(right == left(i)) 45 end do 46 47 print*, "distance: ", dist 48 print*, "similarity: ", sim 49end program day_01 50