Skip to content

Commit

Permalink
Solve day 1, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkram committed Dec 3, 2024
1 parent 1c8828d commit 107193f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 2024/day01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ program day01
integer, allocatable, dimension(:,:) :: input_data
integer :: num_rows, solution_1, solution_2

! Test data
call load_data("test_input.txt", input_data, num_rows, num_cols)

solution_1 = solve_part1(input_data)
call assert_equal(solution_1, 11)

solution_2 = solve_part2(input_data)
call assert_equal(solution_2, 31)

! Reset the allocation
deallocate(input_data)

call load_data("input.txt", input_data, num_rows, num_cols)
Expand All @@ -18,6 +24,11 @@ program day01
print *, "The solution to part 1 is ", solution_1
print *

solution_2 = solve_part2(input_data)

print *, "The solution to part 2 is ", solution_2
print *

contains

subroutine load_data(filename, input_data, num_rows, num_cols)
Expand Down Expand Up @@ -86,6 +97,29 @@ function solve_part1(input_data) result (x)
end do
end function solve_part1

function solve_part2(input_data) result (x)
integer, intent(in), dimension(:,:) :: input_data

integer, dimension(size(input_data, 1)) :: left, right

integer :: x
integer :: num_rows, i, j

num_rows = size(input_data, 1)

left = input_data(:,1)
right = input_data(:,2)

x = 0
do i=1,num_rows
do j=1,num_rows
if (left(i) == right(j)) then
x = x + left(i)
end if
end do
end do
end function solve_part2

recursive subroutine quicksort(a, firstin, lastin)
!
! Modified from: https://gist.github.com/t-nissie/479f0f16966925fa29ea
Expand Down

0 comments on commit 107193f

Please sign in to comment.