diff options
| author | Trey Bastian <2991824+TreyBastian@users.noreply.github.com> | 2024-12-01 23:33:13 +0000 |
|---|---|---|
| committer | Trey Bastian <2991824+TreyBastian@users.noreply.github.com> | 2024-12-01 23:33:13 +0000 |
| commit | b89f1c3291777d32d87ddcec2bf45e3f438f77cc (patch) | |
| tree | 862798ad33bf5695474f8884deb2de392d0502d8 /2024/day_01.f90 | |
| parent | 73db524942bdfb5450f2cd61682626a9618e99dc (diff) | |
day 1 done
Diffstat (limited to '2024/day_01.f90')
| -rw-r--r-- | 2024/day_01.f90 | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/2024/day_01.f90 b/2024/day_01.f90 new file mode 100644 index 0000000..ac4faf1 --- /dev/null +++ b/2024/day_01.f90 @@ -0,0 +1,50 @@ +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 + end do + arr(j+1) = key + end do + 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') + + do i = 1, 1000 + read(io, *) left(i), right(i) ! read both values in line + end do + + close(10) ! close the file + + call sort(left) + call sort(right) + + ! 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 + + print*, "distance: ", dist + print*, "similarity: ", sim +end program day_01 + |
