flatten array of array in grid implementation
makes the new find function much simpler to implement
This commit is contained in:
1 parent
31a75867fa
commit
887d961d29
3 files changed
+39
-15
No files matched your search
@@ -1 +1,2 @@
|
|||||||
#install_printer Aoc.Vec2.pp;;
|
#install_printer Aoc.Vec2.pp;;
|
||||||
|
#install_printer Aoc.Vec2.pp_grid;;
|
||||||
+36
-14
@@ -1,7 +1,13 @@
|
|||||||
open Containers
|
open Containers
|
||||||
|
|
||||||
type t = int * int [@@deriving show]
|
type t = int * int [@@deriving show]
|
||||||
type 'a grid = 'a array array
|
|
||||||
|
type 'a grid =
|
||||||
|
{ width : int
|
||||||
|
; height : int
|
||||||
|
; items : 'a array
|
||||||
|
}
|
||||||
|
[@@deriving show]
|
||||||
|
|
||||||
let up = 0, 1
|
let up = 0, 1
|
||||||
let down = 0, -1
|
let down = 0, -1
|
||||||
@@ -14,24 +20,40 @@ let ( + ) a b = Pair.map_same2 ( + ) a b
|
|||||||
let ( - ) a b = Pair.map_same2 ( - ) a b
|
let ( - ) a b = Pair.map_same2 ( - ) a b
|
||||||
let ( = ) a b = Pair.equal ( = ) ( = ) a b
|
let ( = ) a b = Pair.equal ( = ) ( = ) a b
|
||||||
|
|
||||||
let at grid point =
|
let at (grid : 'a grid) (point : t) =
|
||||||
let x, y = to_tuple point in
|
let x, y = point in
|
||||||
|
if x >= 0 && x < grid.width && y >= 0 && y < grid.height
|
||||||
|
then Array.get_safe grid.items @@ Int.((grid.width * y) + x)
|
||||||
|
else None
|
||||||
|
;;
|
||||||
|
|
||||||
|
let find (grid : 'a grid) pred =
|
||||||
let open Option.Infix in
|
let open Option.Infix in
|
||||||
let* row = Array.get_safe grid y in
|
let* idx, _ = Array.find_idx pred grid.items in
|
||||||
Array.get_safe row x
|
let y = idx / grid.width in
|
||||||
|
let x = idx mod grid.width in
|
||||||
|
Some (x, y)
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let parse_grid parse_char lines =
|
let parse_grid parse_char lines : 'a grid =
|
||||||
lines
|
match lines with
|
||||||
|> Array.of_list
|
| first_line :: _ ->
|
||||||
|> Array.map Fun.(String.to_seq %> Seq.map parse_char %> Seq.to_array)
|
let width = String.length first_line in
|
||||||
|
let height = List.length lines in
|
||||||
|
let yeet =
|
||||||
|
lines
|
||||||
|
|> List.map String.to_list
|
||||||
|
|> List.flatten
|
||||||
|
|> List.map parse_char
|
||||||
|
|> Array.of_list
|
||||||
|
in
|
||||||
|
{ width; height; items = yeet }
|
||||||
|
| _ -> failwith "somethin fucd up"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let iter_grid grid callback =
|
let iter_grid (grid : 'a grid) callback =
|
||||||
let grid_h = Array.length grid in
|
for y = 0 to Int.(grid.height - 1) do
|
||||||
let grid_w = Array.length grid.(0) in
|
for x = 0 to Int.(grid.width - 1) do
|
||||||
for y = 0 to Int.(grid_h - 1) do
|
|
||||||
for x = 0 to Int.(grid_w - 1) do
|
|
||||||
callback (x, y)
|
callback (x, y)
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,5 @@
|
|||||||
type t [@@deriving show]
|
type t [@@deriving show]
|
||||||
type 'a grid
|
type 'a grid [@@deriving show]
|
||||||
|
|
||||||
val up : t
|
val up : t
|
||||||
val down : t
|
val down : t
|
||||||
@@ -13,4 +13,5 @@ val ( - ) : t -> t -> t
|
|||||||
val ( = ) : t -> t -> bool
|
val ( = ) : t -> t -> bool
|
||||||
val at : 'a grid -> t -> 'a option
|
val at : 'a grid -> t -> 'a option
|
||||||
val parse_grid : (char -> 'a) -> string list -> 'a grid
|
val parse_grid : (char -> 'a) -> string list -> 'a grid
|
||||||
|
val find : 'a grid -> ('a -> bool) -> t option
|
||||||
val iter_grid : 'a grid -> (t -> unit) -> unit
|
val iter_grid : 'a grid -> (t -> unit) -> unit
|
||||||
Reference in new issue
Block a user