diff options
Diffstat (limited to '2024/day_01.f90')
| -rw-r--r-- | 2024/day_01.f90 | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/2024/day_01.f90 b/2024/day_01.f90 index ac4faf1..28d0704 100644 --- a/2024/day_01.f90 +++ b/2024/day_01.f90 @@ -1,50 +1,50 @@ -module sorting - ! apparently you need to put the subroutine in a module - implicit none - contains - subroutine sort(arr) +module sorting + ! apparently you need to put the subroutine in a module + implicit none +contains + subroutine sort(arr) !! a little insertion sort KISS !! also yea we have no in built sorting - implicit none - integer, intent(inout) :: arr(1000) - integer :: i, j, key - do i = 2, 1000 - key = arr(i) - j = i - 1 - do while (j > 0 .and. arr(j) > key) - arr(j+1) = arr(j) - j = j - 1 + implicit none + integer, intent(inout) :: arr(1000) + integer :: i, j, key + do i = 2, 1000 + key = arr(i) + j = i - 1 + do while (j > 0 .and. arr(j) > key) + arr(j+1) = arr(j) + j = j - 1 + end do + arr(j+1) = key end do - arr(j+1) = key - end do - end subroutine sort + end subroutine sort end module sorting program day_01 - use sorting - implicit none - integer :: io, i, dist, sim, left(1000), right(1000) ! yea we are coding to the input file and not generic - open(newunit=io, file='./day_01_input.txt', status='old', action='read') + use sorting + implicit none + integer :: io, i, dist, sim, left(1000), right(1000) ! yea we are coding to the input file and not generic + open(newunit=io, file='./day_01_input.txt', status='old', action='read') - do i = 1, 1000 - read(io, *) left(i), right(i) ! read both values in line - end do + do i = 1, 1000 + read(io, *) left(i), right(i) ! read both values in line + end do - close(10) ! close the file + close(10) ! close the file - call sort(left) - call sort(right) + call sort(left) + call sort(right) - ! if we dont init these bad things happen on multiple runs - dist = 0 - sim = 0 + ! if we dont init these bad things happen on multiple runs + dist = 0 + sim = 0 - do i = 1, 1000 - dist = dist + abs(left(i) - right(i)) - sim = sim + left(i) * count(right == left(i)) - end do + do i = 1, 1000 + dist = dist + abs(left(i) - right(i)) + sim = sim + left(i) * count(right == left(i)) + end do - print*, "distance: ", dist - print*, "similarity: ", sim + print*, "distance: ", dist + print*, "similarity: ", sim end program day_01 |
