Moved Grid to Aoc module.

This commit is contained in:
2024-12-10 11:31:02 +00:00
parent 76dc338c9a
commit fe93f65f6a
2 changed files with 82 additions and 1 deletions

View File

@@ -74,3 +74,50 @@ module IntPairSet : sig
val add_seq : elt Seq.t -> t -> t
val of_seq : elt Seq.t -> t
end
(** The [Grid] module is used to represent and manipulate a grid of characters.
Its main goals are to be non-mutable and have constant access times to
locations in the grid.
Grid locations can be accessed by index or (col, row) position. Indicies do
not guarantee an ordering on accesses - but iterating by index from 0 to
[Grid.length grid - 1] inclusive will cover the whole grid. *)
module Grid : sig
type t
(** The type used to represent a grid *)
val of_file : string -> t
(** [Grid.of_file fname] returns a grid loaded from the file [fname] *)
val length : t -> int
(** [Grid.length grid] returns the length of the grid. *)
val get_by_idx : t -> int -> char
(** [Grid.get_by_idx grid idx] returns the character at index [idx] in [grid].
*)
val get_by_pos : t -> int * int -> char
(** [Grid.get_by_pos grid pos] returns the character at position [pos] in
[grid]. *)
val pos_of_idx : t -> int -> int * int
(** [Grid.pos_of_idx grid idx] returns the [(x, y)] position mapped by [idx]
in [grid]. *)
val idx_of_pos : t -> int * int -> int
(** [Grid.pos_of_idx grid pos] returns the index corresponding to [pos] in
[grid]. *)
val pos_is_valid : t -> int * int -> bool
(** [Grid.pos_is_valid grid pos] returns [true] if and only if [pos] is a
valid position in [grid]. *)
val idx_from_opt : t -> int -> char -> int option
(** [Grid.idx_from_opt grid start c] returns [Some idx] where [idx] is the
first location in [grid] at or after the [start] index which is [c]. It
returns [None] if [c] does not appear. *)
val update_pos : t -> int * int -> char -> t
(** [Grid.update_pos grid pos c] returns a grid with the character at position
[pos] changed to [c]. *)
end