Compare commits

...

2 Commits

Author SHA1 Message Date
ryan
19a763b73f manually implement show & pp for Vec2
so that it prints as if it were a tuple
2023-11-08 09:14:22 -08:00
ryan
dcb1338aa3 parse day 15 2023-11-08 09:14:00 -08:00
5 changed files with 118 additions and 4 deletions

36
inputs/day15.tt Normal file
View File

@ -0,0 +1,36 @@
Sensor at x=13820, y=3995710: closest beacon is at x=1532002, y=3577287
Sensor at x=3286002, y=2959504: closest beacon is at x=3931431, y=2926694
Sensor at x=3654160, y=2649422: closest beacon is at x=3702627, y=2598480
Sensor at x=3702414, y=2602790: closest beacon is at x=3702627, y=2598480
Sensor at x=375280, y=2377181: closest beacon is at x=2120140, y=2591883
Sensor at x=3875726, y=2708666: closest beacon is at x=3931431, y=2926694
Sensor at x=3786107, y=2547075: closest beacon is at x=3702627, y=2598480
Sensor at x=2334266, y=3754737: closest beacon is at x=2707879, y=3424224
Sensor at x=1613400, y=1057722: closest beacon is at x=1686376, y=-104303
Sensor at x=3305964, y=2380628: closest beacon is at x=3702627, y=2598480
Sensor at x=1744420, y=3927424: closest beacon is at x=1532002, y=3577287
Sensor at x=3696849, y=2604845: closest beacon is at x=3702627, y=2598480
Sensor at x=2357787, y=401688: closest beacon is at x=1686376, y=-104303
Sensor at x=2127900, y=1984887: closest beacon is at x=2332340, y=2000000
Sensor at x=3705551, y=2604421: closest beacon is at x=3702627, y=2598480
Sensor at x=1783014, y=2978242: closest beacon is at x=2120140, y=2591883
Sensor at x=2536648, y=2910642: closest beacon is at x=2707879, y=3424224
Sensor at x=3999189, y=2989409: closest beacon is at x=3931431, y=2926694
Sensor at x=3939169, y=2382534: closest beacon is at x=3702627, y=2598480
Sensor at x=2792378, y=2002602: closest beacon is at x=2332340, y=2000000
Sensor at x=3520934, y=3617637: closest beacon is at x=2707879, y=3424224
Sensor at x=2614525, y=1628105: closest beacon is at x=2332340, y=2000000
Sensor at x=2828931, y=3996545: closest beacon is at x=2707879, y=3424224
Sensor at x=2184699, y=2161391: closest beacon is at x=2332340, y=2000000
Sensor at x=2272873, y=1816621: closest beacon is at x=2332340, y=2000000
Sensor at x=1630899, y=3675405: closest beacon is at x=1532002, y=3577287
Sensor at x=3683190, y=2619409: closest beacon is at x=3702627, y=2598480
Sensor at x=180960, y=185390: closest beacon is at x=187063, y=-1440697
Sensor at x=1528472, y=3321640: closest beacon is at x=1532002, y=3577287
Sensor at x=3993470, y=2905566: closest beacon is at x=3931431, y=2926694
Sensor at x=1684313, y=20931: closest beacon is at x=1686376, y=-104303
Sensor at x=2547761, y=2464195: closest beacon is at x=2120140, y=2591883
Sensor at x=3711518, y=845968: closest beacon is at x=3702627, y=2598480
Sensor at x=3925049, y=2897039: closest beacon is at x=3931431, y=2926694
Sensor at x=1590740, y=3586256: closest beacon is at x=1532002, y=3577287
Sensor at x=1033496, y=3762565: closest beacon is at x=1532002, y=3577287

65
src/day15.ml Normal file
View File

@ -0,0 +1,65 @@
open Containers
type sensor =
{ pos : Vec2.t
; beacon : Vec2.t
; dist : int
}
[@@deriving show { with_path = false }]
let taxicab_dist a b =
let x, y = Vec2.(abs (a - b) |> to_tuple) in
x + y
;;
let parse_sensor line =
let open Utils.Parse in
let parse =
many1 (not_int *> int)
>>= function
| [ sx; sy; bx; by ] ->
let pos = Vec2.of_tuple (sx, sy) in
let beacon = Vec2.of_tuple (bx, by) in
return { pos; beacon; dist = taxicab_dist pos beacon }
| _ -> failwith "should be four ints"
in
Result.get_or_failwith @@ parse_string ~consume:All parse line
;;
let example_lines =
[ "Sensor at x=2, y=18: closest beacon is at x=-2, y=15"
; "Sensor at x=9, y=16: closest beacon is at x=10, y=16"
; "Sensor at x=13, y=2: closest beacon is at x=15, y=3"
; "Sensor at x=12, y=14: closest beacon is at x=10, y=16"
; "Sensor at x=10, y=20: closest beacon is at x=10, y=16"
; "Sensor at x=14, y=17: closest beacon is at x=10, y=16"
; "Sensor at x=8, y=7: closest beacon is at x=2, y=10"
; "Sensor at x=2, y=0: closest beacon is at x=2, y=10"
; "Sensor at x=0, y=11: closest beacon is at x=2, y=10"
; "Sensor at x=20, y=14: closest beacon is at x=25, y=17"
; "Sensor at x=17, y=20: closest beacon is at x=21, y=22"
; "Sensor at x=16, y=7: closest beacon is at x=15, y=3"
; "Sensor at x=14, y=3: closest beacon is at x=15, y=3"
; "Sensor at x=20, y=1: closest beacon is at x=15, y=3"
]
;;
let%expect_test "Day 15 example" =
example_lines |> List.map parse_sensor |> List.iter @@ Format.printf "%a@ " pp_sensor;
[%expect
{|
{ 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

@ -13,11 +13,20 @@ let lines_of_input day =
module Parse = struct
include Angstrom
let int =
let sign = option 1 (char '-' >>= fun _ -> return (-1))
let digits =
take_while1 (function
| '0' .. '9' -> true
| _ -> false)
>>| int_of_string
<?> "int"
;;
let int = map2 sign digits ~f:( * )
let not_int =
skip_while (function
| '0' .. '9' | '-' -> false
| _ -> true)
;;
end

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 }
@ -17,3 +18,4 @@ let to_tuple { x; y } = x, y
let ( + ) a b = { x = a.x + b.x; y = a.y + b.y }
let ( - ) a b = { x = a.x - b.x; y = a.y - b.y }
let ( = ) a b = a.x = b.x && a.y = b.y
let abs a = Int.{ x = abs a.x; y = abs a.y }

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
@ -15,3 +16,4 @@ val to_tuple : t -> int * int
val ( + ) : t -> t -> t
val ( - ) : t -> t -> t
val ( = ) : t -> t -> bool
val abs : t -> t