diff options
| author | DJ O'Leary <dijitol@proton.me> | 2025-12-08 01:28:45 +0100 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2025-12-08 01:28:45 +0100 |
| commit | 4b3c8eb99443ccaeca831bba8e6604f1036fa301 (patch) | |
| tree | 381a6938aa4df8cab0e926ac0ee96eb192cbf768 /2025/ocaml/lib/domain | |
| parent | 373c53ce69b08c0cf2076dd12ba8b586199a5663 (diff) | |
feat(2025d4): solve part 1
solution: 1320
Diffstat (limited to '2025/ocaml/lib/domain')
| -rw-r--r-- | 2025/ocaml/lib/domain/d04p01.ml | 0 | ||||
| -rw-r--r-- | 2025/ocaml/lib/domain/matrix.ml | 35 |
2 files changed, 33 insertions, 2 deletions
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 -> |
