From 0f34d9a931791406d2cabf282742530195f71927 Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Mon, 8 Dec 2025 01:55:28 +0100 Subject: refactor(2025,ocaml): use result type for answers to allow for returning errors --- 2025/ocaml/lib/application/d02p01.ml | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to '2025/ocaml/lib/application/d02p01.ml') 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 -- cgit v1.2.3