summaryrefslogtreecommitdiff
path: root/2025/ocaml/lib/application/d04p01.ml
diff options
context:
space:
mode:
authorDJ O'Leary <dijitol@proton.me>2025-12-07 02:26:49 +0100
committerDJ O'Leary <dijitol@proton.me>2025-12-07 02:26:49 +0100
commit97a9126507b5381b7f7b87b006bd6414e3ecc9d2 (patch)
tree62af23bd0de066a896e5d829800b10fca7f06e79 /2025/ocaml/lib/application/d04p01.ml
parent26799e3010ce58a268ae9d989f6e3a71b303c509 (diff)
feat(2025d4p1): add code to convert input into a matrix and pp the matrix
Diffstat (limited to '2025/ocaml/lib/application/d04p01.ml')
-rw-r--r--2025/ocaml/lib/application/d04p01.ml40
1 files changed, 39 insertions, 1 deletions
diff --git a/2025/ocaml/lib/application/d04p01.ml b/2025/ocaml/lib/application/d04p01.ml
index 26319df..85b0bbf 100644
--- a/2025/ocaml/lib/application/d04p01.ml
+++ b/2025/ocaml/lib/application/d04p01.ml
@@ -1 +1,39 @@
-let solve _ () = failwith "not started"
+(** [input_to_matrix input] is a 2D Array where each line of the [input] is
+ a row and each character in that line is a column.
+ Requires: all lines in input are of equal length. *)
+let input_to_matrix input =
+ let lines =
+ input |> String.split_on_char '\n'
+ |> List.filter_map (fun line ->
+ let trimmed = String.trim line in
+ if trimmed <> "" then Some trimmed else None )
+ in
+ let row_count = List.length lines in
+ let column_count =
+ match List.nth_opt lines 1 with
+ | None ->
+ failwith "first line should not be empty"
+ | Some line ->
+ String.length line
+ in
+ let matrix = Array.make_matrix column_count row_count "" in
+ List.iteri
+ (fun x line ->
+ String.iteri (fun y char -> matrix.(x).(y) <- Char.escaped char) line )
+ lines ;
+ matrix
+
+let pp_matrix matrix =
+ "[\n"
+ ^ ( matrix
+ |> Array.map @@ Array.fold_left (fun acc c -> acc ^ c) ""
+ |> Array.fold_left
+ (fun acc line ->
+ if acc = "" then " [" ^ line ^ "]" else acc ^ "\n [" ^ line ^ "]" )
+ "" )
+ ^ "\n]"
+
+let solve input () =
+ let matrix = input |> input_to_matrix in
+ Logs.debug (fun m -> m "%s" @@ pp_matrix matrix) ;
+ 0