finished day 12, thx dijkstra !!
This commit is contained in:
1 parent
887d961d29
commit
8562bc8385
4 files changed
+136
-3
No files matched your search
@@ -0,0 +1,87 @@
|
||||
open Containers
|
||||
|
||||
let infinity = 1000000000
|
||||
|
||||
type node =
|
||||
{ char : char
|
||||
; mutable visited : bool
|
||||
; mutable distance : int
|
||||
; mutable previous : (Vec2.t * node) option
|
||||
}
|
||||
[@@deriving show]
|
||||
|
||||
let parse_grid start_anywhere =
|
||||
let s = ref @@ Vec2.of_tuple (0, 0) in
|
||||
let e = ref @@ Vec2.of_tuple (0, 0) in
|
||||
let unvisited = ref [] in
|
||||
let grid =
|
||||
Utils.lines_of_input 12
|
||||
|> Vec2.parse_grid (fun char ->
|
||||
{ char; visited = false; distance = infinity; previous = None })
|
||||
in
|
||||
Vec2.iter_grid grid (fun pos ->
|
||||
let n = Vec2.at_e grid pos in
|
||||
match n.char with
|
||||
| 'E' -> e := pos
|
||||
| 'S' ->
|
||||
n.distance <- 0;
|
||||
s := pos
|
||||
| 'a' when start_anywhere ->
|
||||
n.distance <- 0;
|
||||
unvisited := pos :: !unvisited
|
||||
| _ -> ());
|
||||
grid, !e, !s, !unvisited
|
||||
;;
|
||||
|
||||
let reachable f t =
|
||||
let height c =
|
||||
match c with
|
||||
| 'S' -> 'a'
|
||||
| 'E' -> 'z'
|
||||
| c -> c
|
||||
in
|
||||
Char.compare (height f) (height t) >= -1
|
||||
;;
|
||||
|
||||
let rec path grid goal current unvisited =
|
||||
let current_node = Vec2.at_e grid current in
|
||||
let neighbors = Vec2.directions |> List.map (fun dir -> Vec2.(current + dir)) in
|
||||
neighbors
|
||||
|> List.iter (fun neighbor ->
|
||||
match Vec2.at grid neighbor with
|
||||
| Some node when (not node.visited) && reachable current_node.char node.char ->
|
||||
let new_distance = current_node.distance + 1 in
|
||||
if new_distance < node.distance
|
||||
then (
|
||||
node.distance <- new_distance;
|
||||
node.previous <- Some (current, current_node));
|
||||
if Option.is_none @@ List.find_opt (fun x -> Vec2.(x = neighbor)) !unvisited
|
||||
then unvisited := neighbor :: !unvisited
|
||||
| _ -> ());
|
||||
current_node.visited <- true;
|
||||
unvisited
|
||||
:= List.sort
|
||||
(fun a b ->
|
||||
let a = Vec2.at_e grid a in
|
||||
let b = Vec2.at_e grid b in
|
||||
a.distance - b.distance)
|
||||
!unvisited;
|
||||
match !unvisited with
|
||||
| shortest :: _ when Vec2.(shortest = goal) -> Vec2.at grid shortest
|
||||
| shortest :: rest -> path grid goal shortest @@ ref rest
|
||||
| [] -> None
|
||||
;;
|
||||
|
||||
let%expect_test "Day 12.1" =
|
||||
let grid, e, s, unvisited = parse_grid false in
|
||||
let solution = Option.get_exn_or "coulnd't find" @@ path grid e s @@ ref unvisited in
|
||||
Printf.printf "%d" solution.distance;
|
||||
[%expect {| 497 |}]
|
||||
;;
|
||||
|
||||
let%expect_test "Day 12.2" =
|
||||
let grid, e, s, unvisited = parse_grid true in
|
||||
let solution = Option.get_exn_or "coulnd't find" @@ path grid e s @@ ref unvisited in
|
||||
Printf.printf "%d" solution.distance;
|
||||
[%expect {| 492 |}]
|
||||
;;
|
||||
+6
-2
@@ -27,12 +27,16 @@ let at (grid : 'a grid) (point : t) =
|
||||
else None
|
||||
;;
|
||||
|
||||
let at_e (grid : 'a grid) (point : t) =
|
||||
Option.get_exn_or "out of bounds!" @@ at grid point
|
||||
;;
|
||||
|
||||
let find (grid : 'a grid) pred =
|
||||
let open Option.Infix in
|
||||
let* idx, _ = Array.find_idx pred grid.items in
|
||||
let* idx, item = Array.find_idx pred grid.items in
|
||||
let y = idx / grid.width in
|
||||
let x = idx mod grid.width in
|
||||
Some (x, y)
|
||||
Some ((x, y), item)
|
||||
;;
|
||||
|
||||
let parse_grid parse_char lines : 'a grid =
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ val ( + ) : t -> t -> t
|
||||
val ( - ) : t -> t -> t
|
||||
val ( = ) : t -> t -> bool
|
||||
val at : 'a grid -> t -> 'a option
|
||||
val at_e : 'a grid -> t -> 'a
|
||||
val parse_grid : (char -> 'a) -> string list -> 'a grid
|
||||
val find : 'a grid -> ('a -> bool) -> t option
|
||||
val find : 'a grid -> ('a -> bool) -> (t * 'a) option
|
||||
val iter_grid : 'a grid -> (t -> unit) -> unit
|
||||
Reference in new issue
Block a user