Tidy up code for 2024 day 12.
This still has some mutable state.
This commit is contained in:
@@ -54,12 +54,16 @@ module Grid = struct
|
||||
let length grid = String.length grid.grid
|
||||
let pos_of_idx grid idx = (idx mod grid.width, idx / grid.width)
|
||||
let idx_of_pos grid (x, y) = x + (y * grid.width)
|
||||
let get_by_idx grid idx = grid.grid.[idx]
|
||||
let get_by_pos grid pos = get_by_idx grid (idx_of_pos grid pos)
|
||||
|
||||
let pos_is_valid grid (x, y) =
|
||||
x >= 0 && x < grid.width && y >= 0 && y < grid.height
|
||||
|
||||
let get_by_idx grid idx = grid.grid.[idx]
|
||||
let get_by_pos grid pos = get_by_idx grid (idx_of_pos grid pos)
|
||||
|
||||
let get_by_pos_opt grid pos =
|
||||
if pos_is_valid grid pos then Some (get_by_pos grid pos) else None
|
||||
|
||||
let idx_from_opt grid = String.index_from_opt grid.grid
|
||||
|
||||
let update_pos grid pos c =
|
||||
|
@@ -114,6 +114,10 @@ module Grid : sig
|
||||
(** [Grid.get_by_pos grid pos] returns the character at position [pos] in
|
||||
[grid]. *)
|
||||
|
||||
val get_by_pos_opt : t -> int * int -> char option
|
||||
(** [Grid.get_by_pos_opt grid pos] returns [Some (get_by_pos grid pos)] if
|
||||
[pos] is a valid position in [grid], and [None] otherwise. *)
|
||||
|
||||
val pos_of_idx : t -> int -> int * int
|
||||
(** [Grid.pos_of_idx grid idx] returns the [(x, y)] position mapped by [idx]
|
||||
in [grid]. *)
|
||||
|
Reference in New Issue
Block a user