Compare commits
3
Commits
19a763b73f
...
b89e9c05ef
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b89e9c05ef | ||
|
|
b90478cd03 | ||
|
|
383c6c0fca |
No files matched your search
+2
-12
@@ -44,18 +44,8 @@ let draw_rocks grid points =
|
|||||||
|
|
||||||
let parse_grid lines start =
|
let parse_grid lines start =
|
||||||
let points = List.map parse_line lines in
|
let points = List.map parse_line lines in
|
||||||
let lx, hx, ly, hy =
|
let Vec2.{ x = lx; y = ly }, Vec2.{ x = hx; y = hy } =
|
||||||
points
|
points |> List.flatten |> List.append [ start ] |> Vec2.bounds 0
|
||||||
|> List.flatten
|
|
||||||
|> List.append [ start ]
|
|
||||||
|> List.fold_left
|
|
||||||
(fun (lx, hx, ly, hy) Vec2.{ x; y } ->
|
|
||||||
let lx = min lx x in
|
|
||||||
let hx = max hx x in
|
|
||||||
let ly = min ly y in
|
|
||||||
let hy = max hy y in
|
|
||||||
lx, hx, ly, hy)
|
|
||||||
(Int.max_int, 0, Int.max_int, 0)
|
|
||||||
in
|
in
|
||||||
let pad_floor = 1 in
|
let pad_floor = 1 in
|
||||||
let pad_x = 2 in
|
let pad_x = 2 in
|
||||||
|
|||||||
+26
-18
@@ -44,22 +44,30 @@ let example_lines =
|
|||||||
]
|
]
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let%expect_test "Day 15 example" =
|
let bounds sensors =
|
||||||
example_lines |> List.map parse_sensor |> List.iter @@ Format.printf "%a@ " pp_sensor;
|
let max_dist = sensors |> List.fold_left (fun acc s -> max acc s.dist) 0 in
|
||||||
[%expect
|
sensors
|
||||||
{|
|
|> List.flat_map (fun sensor -> [ sensor.pos; sensor.beacon ])
|
||||||
{ pos = (2, 18); beacon = (-2, 15); dist = 7 }
|
|> Vec2.bounds max_dist
|
||||||
{ pos = (9, 16); beacon = (10, 16); dist = 1 }
|
;;
|
||||||
{ pos = (13, 2); beacon = (15, 3); dist = 3 }
|
|
||||||
{ pos = (12, 14); beacon = (10, 16); dist = 4 }
|
let%expect_test "Day 15.1" =
|
||||||
{ pos = (10, 20); beacon = (10, 16); dist = 4 }
|
let sensors = Utils.lines_of_input 15 |> List.map parse_sensor in
|
||||||
{ pos = (14, 17); beacon = (10, 16); dist = 5 }
|
let scan_row = 2000000 in
|
||||||
{ pos = (8, 7); beacon = (2, 10); dist = 9 }
|
let Vec2.{ x = lx; _ }, Vec2.{ x = hx; _ } = bounds sensors in
|
||||||
{ pos = (2, 0); beacon = (2, 10); dist = 10 }
|
let is_impossible pos =
|
||||||
{ pos = (0, 11); beacon = (2, 10); dist = 3 }
|
sensors
|
||||||
{ pos = (20, 14); beacon = (25, 17); dist = 8 }
|
|> List.map (fun s ->
|
||||||
{ pos = (17, 20); beacon = (21, 22); dist = 6 }
|
let d = taxicab_dist pos s.pos in
|
||||||
{ pos = (16, 7); beacon = (15, 3); dist = 5 }
|
if d <= s.dist && not Vec2.(pos = s.beacon) then 1 else 0)
|
||||||
{ pos = (14, 3); beacon = (15, 3); dist = 1 }
|
|> List.reduce_exn ( + )
|
||||||
{ pos = (20, 1); beacon = (15, 3); dist = 7 } |}]
|
|> Int.sign
|
||||||
|
in
|
||||||
|
Vec2.fold_region
|
||||||
|
Vec2.{ x = lx; y = scan_row }
|
||||||
|
Vec2.{ x = hx; y = scan_row }
|
||||||
|
(fun acc pos -> acc + is_impossible pos)
|
||||||
|
0
|
||||||
|
|> Printf.printf "%i\n";
|
||||||
|
[%expect {| 5525990 |}]
|
||||||
;;
|
;;
|
||||||
+1
-7
@@ -73,13 +73,7 @@ let of_lines parse_char lines : 'a t =
|
|||||||
;;
|
;;
|
||||||
|
|
||||||
let iter_region grid a b callback =
|
let iter_region grid a b callback =
|
||||||
let open Vec2 in
|
Vec2.iter_region a b (fun pos -> if in_bounds grid pos then callback pos)
|
||||||
for y = min a.y b.y to max a.y b.y do
|
|
||||||
for x = min a.x b.x to max a.x b.x do
|
|
||||||
let pos = { x; y } in
|
|
||||||
if in_bounds grid pos then callback pos
|
|
||||||
done
|
|
||||||
done
|
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let iter grid callback =
|
let iter grid callback =
|
||||||
|
|||||||
+33
@@ -19,3 +19,36 @@ 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 = { x = a.x - b.x; y = a.y - b.y }
|
||||||
let ( = ) a b = a.x = b.x && 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 }
|
let abs a = Int.{ x = abs a.x; y = abs a.y }
|
||||||
|
|
||||||
|
let iter_region a b callback =
|
||||||
|
for y = min a.y b.y to max a.y b.y do
|
||||||
|
for x = min a.x b.x to max a.x b.x do
|
||||||
|
callback { x; y }
|
||||||
|
done
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
|
||||||
|
let fold_region a b callback init =
|
||||||
|
let acc = ref init in
|
||||||
|
iter_region a b (fun pos -> acc := callback !acc pos);
|
||||||
|
!acc
|
||||||
|
;;
|
||||||
|
|
||||||
|
let bounds padding points =
|
||||||
|
let lx, hx, ly, hy =
|
||||||
|
points
|
||||||
|
|> List.fold_left
|
||||||
|
(fun (lx, hx, ly, hy) { x; y } ->
|
||||||
|
let lx = min lx x in
|
||||||
|
let hx = max hx x in
|
||||||
|
let ly = min ly y in
|
||||||
|
let hy = max hy y in
|
||||||
|
lx, hx, ly, hy)
|
||||||
|
(Int.max_int, 0, Int.max_int, 0)
|
||||||
|
in
|
||||||
|
let lx = Int.(lx - padding) in
|
||||||
|
let hx = Int.(hx + padding) in
|
||||||
|
let ly = Int.(ly - padding) in
|
||||||
|
let hy = Int.(hy + padding) in
|
||||||
|
of_tuple (lx, ly), of_tuple (hx, hy)
|
||||||
|
;;
|
||||||
@@ -17,3 +17,6 @@ val ( + ) : t -> t -> t
|
|||||||
val ( - ) : t -> t -> t
|
val ( - ) : t -> t -> t
|
||||||
val ( = ) : t -> t -> bool
|
val ( = ) : t -> t -> bool
|
||||||
val abs : t -> t
|
val abs : t -> t
|
||||||
|
val iter_region : t -> t -> (t -> unit) -> unit
|
||||||
|
val fold_region : t -> t -> ('a -> t -> 'a) -> 'a -> 'a
|
||||||
|
val bounds : int -> t list -> t * t
|
||||||
Reference in new issue
Block a user