diff options
| -rw-r--r-- | 2025/ocaml/lib/application/d04p02.ml | 42 | ||||
| -rw-r--r-- | 2025/ocaml/lib/application/d04p02.mli | 4 | ||||
| -rw-r--r-- | 2025/ocaml/lib/presentation/cli.ml | 1 |
3 files changed, 47 insertions, 0 deletions
diff --git a/2025/ocaml/lib/application/d04p02.ml b/2025/ocaml/lib/application/d04p02.ml new file mode 100644 index 0000000..260c9e9 --- /dev/null +++ b/2025/ocaml/lib/application/d04p02.ml @@ -0,0 +1,42 @@ +open Domain + +let rec solve input () = + let matrix = Matrix.M.of_string input in + let count_paper lst = List.length @@ List.filter (fun c -> c <> ".") lst in + let accessible_rolls_of_paper = ref 0 in + for y = 0 to Array.length matrix - 1 do + for x = 0 to Array.length matrix.(y) - 1 do + if matrix.(y).(x) = "." then () + else + let neighbour_coords = Matrix.M.get_valid_neighbours matrix x y in + let neighbours = + List.map (fun (x', y') -> Matrix.M.get matrix x' y') neighbour_coords + in + let paper_count = count_paper neighbours in + let is_accessible = paper_count < 4 in + if is_accessible then + accessible_rolls_of_paper := !accessible_rolls_of_paper + 1 ; + print_iteration_debug_msg x y neighbour_coords neighbours paper_count + is_accessible + done + done ; + !accessible_rolls_of_paper + +and print_iteration_debug_msg x y neighbour_coords neighbours paper_count + is_accessible = + Logs.debug (fun m -> + m + "coords=(%d,%d) neighbour_coords=[%s] neighbours=[%s] paper=%d \ + is_accessible=%B" + x y + (List.fold_left + (fun acc (x, y) -> + if acc = "" then + " (" ^ string_of_int x ^ "," ^ string_of_int y ^ "); " + else acc ^ "(" ^ string_of_int x ^ "," ^ string_of_int y ^ "); " ) + "" neighbour_coords ) + (List.fold_left + (fun acc neighbour -> + if acc = "" then " " ^ neighbour ^ "; " else acc ^ neighbour ^ "; " ) + "" neighbours ) + paper_count is_accessible ) diff --git a/2025/ocaml/lib/application/d04p02.mli b/2025/ocaml/lib/application/d04p02.mli new file mode 100644 index 0000000..69c5181 --- /dev/null +++ b/2025/ocaml/lib/application/d04p02.mli @@ -0,0 +1,4 @@ +val solve : string -> unit -> int +(** [solve input ()] is the solution for the second part of the fourth day of advent of code 2025 for [input]. + The solution for an empty input is 0. + Requires: [input] to be a string. *) diff --git a/2025/ocaml/lib/presentation/cli.ml b/2025/ocaml/lib/presentation/cli.ml index 1a8e7f7..371f199 100644 --- a/2025/ocaml/lib/presentation/cli.ml +++ b/2025/ocaml/lib/presentation/cli.ml @@ -23,6 +23,7 @@ let run_day _ = function | 4 -> let filepath = "../_input/day_04.txt" in run_solver "D04P01" Application.D04p01.solve filepath ; + run_solver "D04P02" Application.D04p02.solve filepath ; 0 | _ -> failwith "day not implemented" |
