add Vec2.origin for (0,0)
This commit is contained in:
parent
8562bc8385
commit
dfa2cf11a1
17
src/day12.ml
17
src/day12.ml
|
|
@ -6,18 +6,16 @@ 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 s = ref @@ Vec2.origin in
|
||||
let e = ref @@ Vec2.origin in
|
||||
let unvisited = ref [] in
|
||||
let grid =
|
||||
Utils.lines_of_input 12
|
||||
|> Vec2.parse_grid (fun char ->
|
||||
{ char; visited = false; distance = infinity; previous = None })
|
||||
|> Vec2.parse_grid (fun char -> { char; visited = false; distance = infinity })
|
||||
in
|
||||
Vec2.iter_grid grid (fun pos ->
|
||||
let n = Vec2.at_e grid pos in
|
||||
|
|
@ -45,16 +43,13 @@ let reachable f t =
|
|||
|
||||
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
|
||||
Vec2.directions
|
||||
|> List.map (fun dir -> Vec2.(current + dir))
|
||||
|> 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 new_distance < node.distance then node.distance <- new_distance;
|
||||
if Option.is_none @@ List.find_opt (fun x -> Vec2.(x = neighbor)) !unvisited
|
||||
then unvisited := neighbor :: !unvisited
|
||||
| _ -> ());
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ let next_pos head tail =
|
|||
|
||||
let solve length =
|
||||
let moves = parse_input () in
|
||||
let start = Vec2.of_tuple (0, 0) in
|
||||
let start = Vec2.origin in
|
||||
let tail = List.init (length - 1) (fun _ -> start) in
|
||||
let _, _, last_knot_hist =
|
||||
List.fold_left
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ type 'a grid =
|
|||
}
|
||||
[@@deriving show]
|
||||
|
||||
let origin = 0, 0
|
||||
let up = 0, 1
|
||||
let down = 0, -1
|
||||
let left = -1, 0
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
type t [@@deriving show]
|
||||
type 'a grid [@@deriving show]
|
||||
|
||||
val origin : t
|
||||
val up : t
|
||||
val down : t
|
||||
val left : t
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user