summaryrefslogtreecommitdiff
path: root/2025/ocaml/lib/application/d03p01.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/d03p01.ml
parent63c4dea442acb32db82318b4399ac05fb955e6d0 (diff)
refactor(2025,ocaml): use result type for answers to allow for returning errors
Diffstat (limited to '2025/ocaml/lib/application/d03p01.ml')
-rw-r--r--2025/ocaml/lib/application/d03p01.ml46
1 files changed, 28 insertions, 18 deletions
diff --git a/2025/ocaml/lib/application/d03p01.ml b/2025/ocaml/lib/application/d03p01.ml
index 691fe65..14f49e8 100644
--- a/2025/ocaml/lib/application/d03p01.ml
+++ b/2025/ocaml/lib/application/d03p01.ml
@@ -28,20 +28,27 @@
open Domain.D03p01
+let err_empty_input = "input is empty"
+
let to_lines s =
s |> String.split_on_char '\n' |> List.map String.trim
|> List.filter (fun l -> l <> "")
let solve input () =
- input |> to_lines |> List.to_seq
- |> Seq.map BatteryBank.of_string
- |> Seq.mapi (fun idx bb ->
- let joltage = BatteryBank.highest_joltage bb in
- Logs.debug (fun m ->
- m "%d line=%s highest_joltage=%d" (idx + 1) (BatteryBank.pp bb)
- joltage ) ;
- joltage )
- |> Seq.fold_left ( + ) 0
+ if input = "" then Error err_empty_input
+ else
+ let answer =
+ input |> to_lines |> List.to_seq
+ |> Seq.map BatteryBank.of_string
+ |> Seq.mapi (fun idx bb ->
+ let joltage = BatteryBank.highest_joltage bb in
+ Logs.debug (fun m ->
+ m "%d line=%s highest_joltage=%d" (idx + 1) (BatteryBank.pp bb)
+ joltage ) ;
+ joltage )
+ |> Seq.fold_left ( + ) 0
+ in
+ Ok answer
let print_occurring_digits input () =
input |> to_lines |> List.to_seq
@@ -52,14 +59,17 @@ let print_occurring_digits input () =
|> Seq.iteri (fun idx bb ->
Logs.debug (fun m -> m "%d %s" (idx + 1) (BatteryBank.pp bb)) ) ;
-1
+(* disables unused function warning *)
+[@@warning "-32"]
-[@@warning "-32"] (* disables unused function warning *)
let _solve' input () =
- let banks =
- input |> to_lines |> List.to_seq
- |> Seq.map BatteryBank.of_string
- |> Seq.map BatteryBank.to_list
- |> Seq.map (fun bb -> List.map Battery.to_int bb)
- |> List.of_seq
- in
- banks
+ if input = "" then Error err_empty_input
+ else
+ let banks =
+ input |> to_lines |> List.to_seq
+ |> Seq.map BatteryBank.of_string
+ |> Seq.map BatteryBank.to_list
+ |> Seq.map (fun bb -> List.map Battery.to_int bb)
+ |> List.of_seq
+ in
+ Ok banks