From b89f1c3291777d32d87ddcec2bf45e3f438f77cc Mon Sep 17 00:00:00 2001 From: Trey Bastian <2991824+TreyBastian@users.noreply.github.com> Date: Sun, 1 Dec 2024 23:33:13 +0000 Subject: day 1 done --- 2024/day_01.f90 | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2024/day_01.f90 (limited to '2024/day_01.f90') 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 + -- cgit v1.2.3