move vec2 into a new file & add a pretty printer

now it shows up properly in utop !!
This commit is contained in:
ryan 2023-10-05 15:55:05 -07:00
parent 239af2f93b
commit 5e1c30637e
5 changed files with 39 additions and 24 deletions

1
.ocamlinit Normal file
View File

@ -0,0 +1 @@
#install_printer Aoc.Vec2.pp;;

View File

@ -1,28 +1,8 @@
open Containers
module Vec2 : sig
type t
val directions : t list
val of_tuple : int * int -> t
val to_tuple : t -> int * int
val ( + ) : t -> t -> t
val at : 'a array array -> t -> 'a option
end = struct
type t = int * int
let directions = [ 1, 0; 0, 1; -1, 0; 0, -1 ]
let of_tuple x = x
let to_tuple x = x
let ( + ) a b = Pair.map_same2 ( + ) a b
let at grid point =
let x, y = to_tuple point in
let open Option.Infix in
let* row = Array.get_safe grid y in
Array.get_safe row x
;;
end
(* module Vec2 : sig *)
(* end = struct *)
(* end *)
let parse_grid () =
let lines = Utils.lines_of_input 8 in

View File

@ -1,7 +1,7 @@
(library
(name aoc)
(libraries re containers dune-site)
(preprocess (pps ppx_regexp)))
(preprocess (pps ppx_regexp ppx_deriving.show)))
(env
(dev

21
src/vec2.ml Normal file
View File

@ -0,0 +1,21 @@
open Containers
type t = int * int [@@deriving show]
let up = 0, 1
let down = 0, -1
let left = -1, 0
let right = 1, 0
let directions = [ up; down; left; right ]
let of_tuple x = x
let to_tuple x = x
let ( + ) a b = Pair.map_same2 ( + ) a b
let ( - ) a b = Pair.map_same2 ( - ) a b
let ( = ) a b = Pair.equal ( = ) ( = ) a b
let at grid point =
let x, y = to_tuple point in
let open Option.Infix in
let* row = Array.get_safe grid y in
Array.get_safe row x
;;

13
src/vec2.mli Normal file
View File

@ -0,0 +1,13 @@
type t [@@deriving show] [@@ocaml.toplevel_printer]
val up : t
val down : t
val left : t
val right : t
val directions : t list
val of_tuple : int * int -> t
val to_tuple : t -> int * int
val ( + ) : t -> t -> t
val ( - ) : t -> t -> t
val ( = ) : t -> t -> bool
val at : 'a array array -> t -> 'a option