summaryrefslogtreecommitdiff
path: root/2025/ocaml/lib/application/d02p01.ml
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2025-12-08 01:55:28 +0100
committerDJ O'Leary <dijitol@proton.me>2025-12-08 01:57:20 +0100
commit0f34d9a931791406d2cabf282742530195f71927 (patch)
tree076e546f64f8f541e91bf135d5d5c78cde173ccf /2025/ocaml/lib/application/d02p01.ml
parent63c4dea442acb32db82318b4399ac05fb955e6d0 (diff)
refactor(2025,ocaml): use result type for answers to allow for returning errors
Diffstat (limited to '2025/ocaml/lib/application/d02p01.ml')
-rw-r--r--2025/ocaml/lib/application/d02p01.ml23
1 files changed, 15 insertions, 8 deletions
diff --git a/2025/ocaml/lib/application/d02p01.ml b/2025/ocaml/lib/application/d02p01.ml
index ce8778d..f313149 100644
--- a/2025/ocaml/lib/application/d02p01.ml
+++ b/2025/ocaml/lib/application/d02p01.ml
@@ -1,5 +1,7 @@
open Domain.D02p01
+let err_empty_input = "input is empty"
+
(** [input_to_lines input] is a list of strings that have been split on ','.
Example: [input_to_lines "12-34,56-78,90-100"] becomes the list \[ "12-34"; "56-78"; "90-100" \].
Requires: [input] to be a string in the format "12-34,56-78,90-100". *)
@@ -37,11 +39,16 @@ let is_invalid = ID.validate rules
let passthrough_with_logs logger lst = logger lst ; lst
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.filter is_invalid
- |> Seq.fold_left (fun acc id -> acc + ID.to_int 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.filter is_invalid
+ |> Seq.fold_left (fun acc id -> acc + ID.to_int id) 0
+ in
+ Ok answer