···
2
+
use iso_fortran_env, only: int64
5
+
recursive function test_values(arr, target, next_idx, current_value) result(pass)
7
+
integer(kind=int64), intent(in) :: target, current_value, arr(:)
8
+
integer, intent(in) :: next_idx
10
+
if(next_idx > size(arr)) then
11
+
pass = current_value == target
14
+
pass = test_values(arr, target, next_idx + 1, current_value + arr(next_idx)) .or. &
15
+
test_values(arr, target, next_idx + 1, current_value * arr(next_idx))
16
+
end function test_values
17
+
logical function is_calibrated(arr, target)
19
+
integer(kind=int64), intent(in) :: target, arr(:)
20
+
is_calibrated = test_values(arr, target, 2, arr(1))
21
+
end function is_calibrated
22
+
end module day_07_utils
24
+
use iso_fortran_env, only: int64
27
+
integer(kind=int64) :: test_number, res
28
+
integer(kind=int64), allocatable:: work(:)
29
+
integer :: io, ios, idx, i, ct
30
+
character(len=200) :: line, work_line
33
+
open(newunit=io, file='./day_07_input.txt', status='old', action='read')
35
+
read(io, '(a)', iostat=ios) line
37
+
idx = index(line, ':')
39
+
read(line(1:idx-1), *) test_number
41
+
work_line = trim(line(idx+1:len(trim(line))))
42
+
do i = 1 , len(trim(work_line))
43
+
if(work_line(i:i) == ' ') then
48
+
read(work_line, *) work(:)
50
+
pass = is_calibrated(work, test_number)
52
+
res = res + test_number
56
+
print*, "Result: ", res