···
+
use iso_fortran_env, only: int64
+
recursive function test_values(arr, target, next_idx, current_value) result(pass)
+
integer(kind=int64), intent(in) :: target, current_value, arr(:)
+
integer, intent(in) :: next_idx
+
if(next_idx > size(arr)) then
+
pass = current_value == target
+
pass = test_values(arr, target, next_idx + 1, current_value + arr(next_idx)) .or. &
+
test_values(arr, target, next_idx + 1, current_value * arr(next_idx))
+
end function test_values
+
logical function is_calibrated(arr, target)
+
integer(kind=int64), intent(in) :: target, arr(:)
+
is_calibrated = test_values(arr, target, 2, arr(1))
+
end function is_calibrated
+
end module day_07_utils
+
use iso_fortran_env, only: int64
+
integer(kind=int64) :: test_number, res
+
integer(kind=int64), allocatable:: work(:)
+
integer :: io, ios, idx, i, ct
+
character(len=200) :: line, work_line
+
open(newunit=io, file='./day_07_input.txt', status='old', action='read')
+
read(io, '(a)', iostat=ios) line
+
read(line(1:idx-1), *) test_number
+
work_line = trim(line(idx+1:len(trim(line))))
+
do i = 1 , len(trim(work_line))
+
if(work_line(i:i) == ' ') then
+
read(work_line, *) work(:)
+
pass = is_calibrated(work, test_number)
+
res = res + test_number
+
print*, "Result: ", res