manually implement show & pp for Vec2

so that it prints as if it were a tuple
This commit is contained in:
ryan 2023-11-08 09:14:22 -08:00
parent dcb1338aa3
commit 19a763b73f
3 changed files with 18 additions and 16 deletions

View File

@ -48,18 +48,18 @@ let%expect_test "Day 15 example" =
example_lines |> List.map parse_sensor |> List.iter @@ Format.printf "%a@ " pp_sensor;
[%expect
{|
{ pos = { Vec2.x = 2; y = 18 }; beacon = { Vec2.x = -2; y = 15 }; dist = 7 }
{ pos = { Vec2.x = 9; y = 16 }; beacon = { Vec2.x = 10; y = 16 }; dist = 1 }
{ pos = { Vec2.x = 13; y = 2 }; beacon = { Vec2.x = 15; y = 3 }; dist = 3 }
{ pos = { Vec2.x = 12; y = 14 }; beacon = { Vec2.x = 10; y = 16 }; dist = 4 }
{ pos = { Vec2.x = 10; y = 20 }; beacon = { Vec2.x = 10; y = 16 }; dist = 4 }
{ pos = { Vec2.x = 14; y = 17 }; beacon = { Vec2.x = 10; y = 16 }; dist = 5 }
{ pos = { Vec2.x = 8; y = 7 }; beacon = { Vec2.x = 2; y = 10 }; dist = 9 }
{ pos = { Vec2.x = 2; y = 0 }; beacon = { Vec2.x = 2; y = 10 }; dist = 10 }
{ pos = { Vec2.x = 0; y = 11 }; beacon = { Vec2.x = 2; y = 10 }; dist = 3 }
{ pos = { Vec2.x = 20; y = 14 }; beacon = { Vec2.x = 25; y = 17 }; dist = 8 }
{ pos = { Vec2.x = 17; y = 20 }; beacon = { Vec2.x = 21; y = 22 }; dist = 6 }
{ pos = { Vec2.x = 16; y = 7 }; beacon = { Vec2.x = 15; y = 3 }; dist = 5 }
{ pos = { Vec2.x = 14; y = 3 }; beacon = { Vec2.x = 15; y = 3 }; dist = 1 }
{ pos = { Vec2.x = 20; y = 1 }; beacon = { Vec2.x = 15; y = 3 }; dist = 7 } |}]
{ pos = (2, 18); beacon = (-2, 15); dist = 7 }
{ pos = (9, 16); beacon = (10, 16); dist = 1 }
{ pos = (13, 2); beacon = (15, 3); dist = 3 }
{ pos = (12, 14); beacon = (10, 16); dist = 4 }
{ pos = (10, 20); beacon = (10, 16); dist = 4 }
{ pos = (14, 17); beacon = (10, 16); dist = 5 }
{ pos = (8, 7); beacon = (2, 10); dist = 9 }
{ pos = (2, 0); beacon = (2, 10); dist = 10 }
{ pos = (0, 11); beacon = (2, 10); dist = 3 }
{ pos = (20, 14); beacon = (25, 17); dist = 8 }
{ pos = (17, 20); beacon = (21, 22); dist = 6 }
{ pos = (16, 7); beacon = (15, 3); dist = 5 }
{ pos = (14, 3); beacon = (15, 3); dist = 1 }
{ pos = (20, 1); beacon = (15, 3); dist = 7 } |}]
;;

View File

@ -4,8 +4,9 @@ type t =
{ x : int
; y : int
}
[@@deriving show]
let pp (fmt : Format.formatter) v = Format.fprintf fmt "(%i, %i)" v.x v.y
let show v = Format.asprintf "%a" pp v
let origin = { x = 0; y = 0 }
let up = { x = 0; y = -1 }
let down = { x = 0; y = 1 }

View File

@ -2,8 +2,9 @@ type t =
{ x : int
; y : int
}
[@@deriving show]
val show : t -> string
val pp : Format.formatter -> t -> unit
val origin : t
val up : t
val down : t