From 5e1c30637ecac07b457d64de2f8ae498e780769e Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 5 Oct 2023 15:55:05 -0700 Subject: [PATCH] move vec2 into a new file & add a pretty printer now it shows up properly in utop !! --- .ocamlinit | 1 + src/day8.ml | 26 +++----------------------- src/dune | 2 +- src/vec2.ml | 21 +++++++++++++++++++++ src/vec2.mli | 13 +++++++++++++ 5 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 .ocamlinit create mode 100644 src/vec2.ml create mode 100644 src/vec2.mli diff --git a/.ocamlinit b/.ocamlinit new file mode 100644 index 0000000..f0145ae --- /dev/null +++ b/.ocamlinit @@ -0,0 +1 @@ +#install_printer Aoc.Vec2.pp;; diff --git a/src/day8.ml b/src/day8.ml index fabe7ea..2630c31 100644 --- a/src/day8.ml +++ b/src/day8.ml @@ -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 diff --git a/src/dune b/src/dune index 7fa8ed9..35778f6 100644 --- a/src/dune +++ b/src/dune @@ -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 diff --git a/src/vec2.ml b/src/vec2.ml new file mode 100644 index 0000000..020b938 --- /dev/null +++ b/src/vec2.ml @@ -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 +;; diff --git a/src/vec2.mli b/src/vec2.mli new file mode 100644 index 0000000..eb3eb30 --- /dev/null +++ b/src/vec2.mli @@ -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