summaryrefslogtreecommitdiff
path: root/ocaml/lib/year2025/day06
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2026-06-27 00:11:43 +0200
committerDJ O'Leary <dijitol@proton.me>2026-06-27 00:11:43 +0200
commit976048a85403fbb0751c34fa42703a3c47f6e918 (patch)
tree46e321d8120c45e318ead66f60165b0b854c85a1 /ocaml/lib/year2025/day06
parent6d8fb621d1e93b83786890d6d9cb6f0d8defa8ae (diff)
refactor!: change repo directory structure for ocaml
Diffstat (limited to 'ocaml/lib/year2025/day06')
-rw-r--r--ocaml/lib/year2025/day06/part01/solver.ml50
-rw-r--r--ocaml/lib/year2025/day06/part01/solver.mli4
-rw-r--r--ocaml/lib/year2025/day06/part02/solver.ml50
-rw-r--r--ocaml/lib/year2025/day06/part02/solver.mli4
4 files changed, 108 insertions, 0 deletions
diff --git a/ocaml/lib/year2025/day06/part01/solver.ml b/ocaml/lib/year2025/day06/part01/solver.ml
new file mode 100644
index 0000000..d992ac2
--- /dev/null
+++ b/ocaml/lib/year2025/day06/part01/solver.ml
@@ -0,0 +1,50 @@
+let err_empty_input = "input is empty"
+
+let log_matrix matrix =
+ let open Shared.Matrix in
+ Logs.debug (fun m -> m "%s" (M.of_list matrix |> M.pp (fun elt -> elt)))
+
+let log_list lst =
+ Logs.debug (fun m ->
+ m "[%s]" (List.fold_left (fun acc i -> acc ^ i ^ "; ") " " lst) )
+
+type operation = Mult | Add
+
+let solve input () =
+ if input = "" then Error err_empty_input
+ else
+ let input' = Shared.Parser.columns_of_string input in
+ assert (input' <> []) ;
+ log_matrix input' ;
+ let nums =
+ List.map (List.filter (fun i -> i <> "*" && i <> "+")) input'
+ |> List.map
+ @@ List.map (fun n ->
+ Logs.debug (fun m -> m "peeking: %s" n) ;
+ int_of_string n )
+ and ops =
+ List.map (List.filter (fun i -> i = "*" || i = "+")) input'
+ |> List.concat
+ |> List.map (fun op ->
+ match op with
+ | "*" ->
+ Mult
+ | "+" ->
+ Add
+ | _ ->
+ failwith "case not covered" )
+ in
+ let answer =
+ List.map2
+ (fun op num_lst ->
+ match op with
+ | Add ->
+ List.fold_left (fun acc i -> acc + i) 0 num_lst
+ | Mult ->
+ List.fold_left (fun acc i -> acc * i) 1 num_lst )
+ ops nums
+ |> fun lst ->
+ log_list @@ List.map string_of_int lst ;
+ lst |> List.fold_left ( + ) 0
+ in
+ Ok answer
diff --git a/ocaml/lib/year2025/day06/part01/solver.mli b/ocaml/lib/year2025/day06/part01/solver.mli
new file mode 100644
index 0000000..b3abc1e
--- /dev/null
+++ b/ocaml/lib/year2025/day06/part01/solver.mli
@@ -0,0 +1,4 @@
+val solve : string -> unit -> (int, string) result
+(** [solve input ()] is the solution for the first part of the sixth day of advent of code 2025 for [input].
+ The solution for an empty input is [Error msg].
+ Requires: [input] to be a string. *)
diff --git a/ocaml/lib/year2025/day06/part02/solver.ml b/ocaml/lib/year2025/day06/part02/solver.ml
new file mode 100644
index 0000000..d992ac2
--- /dev/null
+++ b/ocaml/lib/year2025/day06/part02/solver.ml
@@ -0,0 +1,50 @@
+let err_empty_input = "input is empty"
+
+let log_matrix matrix =
+ let open Shared.Matrix in
+ Logs.debug (fun m -> m "%s" (M.of_list matrix |> M.pp (fun elt -> elt)))
+
+let log_list lst =
+ Logs.debug (fun m ->
+ m "[%s]" (List.fold_left (fun acc i -> acc ^ i ^ "; ") " " lst) )
+
+type operation = Mult | Add
+
+let solve input () =
+ if input = "" then Error err_empty_input
+ else
+ let input' = Shared.Parser.columns_of_string input in
+ assert (input' <> []) ;
+ log_matrix input' ;
+ let nums =
+ List.map (List.filter (fun i -> i <> "*" && i <> "+")) input'
+ |> List.map
+ @@ List.map (fun n ->
+ Logs.debug (fun m -> m "peeking: %s" n) ;
+ int_of_string n )
+ and ops =
+ List.map (List.filter (fun i -> i = "*" || i = "+")) input'
+ |> List.concat
+ |> List.map (fun op ->
+ match op with
+ | "*" ->
+ Mult
+ | "+" ->
+ Add
+ | _ ->
+ failwith "case not covered" )
+ in
+ let answer =
+ List.map2
+ (fun op num_lst ->
+ match op with
+ | Add ->
+ List.fold_left (fun acc i -> acc + i) 0 num_lst
+ | Mult ->
+ List.fold_left (fun acc i -> acc * i) 1 num_lst )
+ ops nums
+ |> fun lst ->
+ log_list @@ List.map string_of_int lst ;
+ lst |> List.fold_left ( + ) 0
+ in
+ Ok answer
diff --git a/ocaml/lib/year2025/day06/part02/solver.mli b/ocaml/lib/year2025/day06/part02/solver.mli
new file mode 100644
index 0000000..49444de
--- /dev/null
+++ b/ocaml/lib/year2025/day06/part02/solver.mli
@@ -0,0 +1,4 @@
+val solve : string -> unit -> (int, string) result
+(** [solve input ()] is the solution for the second part of the sixth day of advent of code 2025 for [input].
+ The solution for an empty input is [Error msg].
+ Requires: [input] to be a string. *)