diff options
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/ocaml/lib/application/d06p02.ml | 50 | ||||
| -rw-r--r-- | 2025/ocaml/lib/application/d06p02.mli | 4 | ||||
| -rw-r--r-- | 2025/ocaml/lib/presentation/cli.ml | 1 |
3 files changed, 55 insertions, 0 deletions
diff --git a/2025/ocaml/lib/application/d06p02.ml b/2025/ocaml/lib/application/d06p02.ml new file mode 100644 index 0000000..9425810 --- /dev/null +++ b/2025/ocaml/lib/application/d06p02.ml @@ -0,0 +1,50 @@ +let err_empty_input = "input is empty" + +let log_matrix matrix = + let open Domain.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' = 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/2025/ocaml/lib/application/d06p02.mli b/2025/ocaml/lib/application/d06p02.mli new file mode 100644 index 0000000..49444de --- /dev/null +++ b/2025/ocaml/lib/application/d06p02.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. *) diff --git a/2025/ocaml/lib/presentation/cli.ml b/2025/ocaml/lib/presentation/cli.ml index 4d979b0..f4c57e6 100644 --- a/2025/ocaml/lib/presentation/cli.ml +++ b/2025/ocaml/lib/presentation/cli.ml @@ -35,6 +35,7 @@ let run_day _ = function | 6 -> let filepath = "../_input/day_06.txt" in run_solver "D06P01" Application.D06p01.solve filepath ; + run_solver "D06P02" Application.D06p02.solve filepath ; 0 | _ -> failwith "day not implemented" |
