diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-12-08 01:55:28 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-12-08 01:57:20 +0100 |
| commit | 0f34d9a931791406d2cabf282742530195f71927 (patch) | |
| tree | 076e546f64f8f541e91bf135d5d5c78cde173ccf /2025/ocaml/lib/application/d02p02.ml | |
| parent | 63c4dea442acb32db82318b4399ac05fb955e6d0 (diff) | |
refactor(2025,ocaml): use result type for answers to allow for returning errors
Diffstat (limited to '2025/ocaml/lib/application/d02p02.ml')
| -rw-r--r-- | 2025/ocaml/lib/application/d02p02.ml | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/2025/ocaml/lib/application/d02p02.ml b/2025/ocaml/lib/application/d02p02.ml index 29e3877..d062f7b 100644 --- a/2025/ocaml/lib/application/d02p02.ml +++ b/2025/ocaml/lib/application/d02p02.ml @@ -1,5 +1,9 @@ open Domain.D02p02 +(* Errors *) + +let err_empty_input = "input is empty" + (* Logging *) (** [passthrough_with_logs logger lst] is [lst] with the side-effects caused by calling [logger] on [lst]. *) @@ -67,11 +71,16 @@ let is_invalid id = List.length repeating_patterns > 0 let solve input () = - input |> input_to_lines |> List.to_seq - |> passthrough_with_logs pp_lines - |> lines_to_ranges - |> passthrough_with_logs pp_ranges - |> collect_all - |> passthrough_with_logs pp_collection - |> Seq.map ID.to_string |> Seq.filter is_invalid - |> Seq.fold_left (fun acc id -> acc + int_of_string id) 0 + if input = "" then Error err_empty_input + else + let answer = + input |> input_to_lines |> List.to_seq + |> passthrough_with_logs pp_lines + |> lines_to_ranges + |> passthrough_with_logs pp_ranges + |> collect_all + |> passthrough_with_logs pp_collection + |> Seq.map ID.to_string |> Seq.filter is_invalid + |> Seq.fold_left (fun acc id -> acc + int_of_string id) 0 + in + Ok answer |
