summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2025/ocaml/lib/application/d04p01.ml46
-rw-r--r--2025/ocaml/lib/domain/d04p01.ml0
-rw-r--r--2025/ocaml/lib/domain/matrix.ml35
3 files changed, 75 insertions, 6 deletions
diff --git a/2025/ocaml/lib/application/d04p01.ml b/2025/ocaml/lib/application/d04p01.ml
index 4bf38ad..260c9e9 100644
--- a/2025/ocaml/lib/application/d04p01.ml
+++ b/2025/ocaml/lib/application/d04p01.ml
@@ -1,4 +1,42 @@
-let solve input () =
- let matrix = Domain.Matrix.M.of_string input in
- Logs.debug (fun m -> m "\n%s" @@ Domain.Matrix.M.pp (fun s -> s) matrix) ;
- 0
+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/domain/d04p01.ml b/2025/ocaml/lib/domain/d04p01.ml
deleted file mode 100644
index e69de29..0000000
--- a/2025/ocaml/lib/domain/d04p01.ml
+++ /dev/null
diff --git a/2025/ocaml/lib/domain/matrix.ml b/2025/ocaml/lib/domain/matrix.ml
index bb703fc..8983287 100644
--- a/2025/ocaml/lib/domain/matrix.ml
+++ b/2025/ocaml/lib/domain/matrix.ml
@@ -6,13 +6,23 @@ module M : sig
where each element in the array is initialized to [initial]. *)
val of_list : 'a list list -> 'a t
- (** *)
+ (** [of_list lst] is a 2D array where every element in the list is an element. *)
val of_string : string -> string t
(** [of_string str] is a 2D array where every character is an element. *)
val get : 'a t -> int -> int -> 'a
- (** [get matrix x y] is an alias for [matrix.(y).(x)] *)
+ (** [get matrix x y] is an alias for [matrix.(y).(x)].
+ Requires: [x] and [y] are positive integers or 0. *)
+
+ val set : 'a t -> int -> int -> 'a -> unit
+ (** [set matrix x y v] is an alias for [matrix.(y).(x) <- v].
+ Requires: [x] and [y] are positive integers or 0. *)
+
+ val get_valid_neighbours : 'a t -> int -> int -> (int * int) list
+ (** [get_valid_neighbours matrix x y] is a list of pairs in the format
+ (x * y) that represent the valid surrounding positions of [matrix.(y).(x)].
+ Requires: [x] and [y] are positive integers or 0. *)
val pp : ('a -> string) -> 'a t -> string
(** [pp matrix string_of_elt] is the string representation of the [matrix]
@@ -40,6 +50,27 @@ end = struct
let get matrix x y = matrix.(y).(x)
+ let set matrix x y v = matrix.(y).(x) <- v
+
+ let get_valid_neighbours matrix x y =
+ let potential_neighbours =
+ [ (x - 1, y - 1)
+ ; (x, y - 1)
+ ; (x + 1, y - 1)
+ ; (x - 1, y)
+ ; (x + 1, y)
+ ; (x - 1, y + 1)
+ ; (x, y + 1)
+ ; (x + 1, y + 1) ]
+ in
+ let min_x = 0 in
+ let max_x = Array.length matrix.(y) - 1 in
+ let min_y = 0 in
+ let max_y = Array.length matrix - 1 in
+ List.filter
+ (fun (x, y) -> min_x <= x && x <= max_x && min_y <= y && y <= max_y)
+ potential_neighbours
+
let pp string_of_elt matrix =
Array.fold_left
(fun col_acc arr ->