add Vec2.origin for (0,0)
This commit is contained in:
1 parent
8562bc8385
commit
dfa2cf11a1
4 files changed
+9
-12
No files matched your search
+6
-11
@@ -6,18 +6,16 @@ type node =
|
|||||||
{ char : char
|
{ char : char
|
||||||
; mutable visited : bool
|
; mutable visited : bool
|
||||||
; mutable distance : int
|
; mutable distance : int
|
||||||
; mutable previous : (Vec2.t * node) option
|
|
||||||
}
|
}
|
||||||
[@@deriving show]
|
[@@deriving show]
|
||||||
|
|
||||||
let parse_grid start_anywhere =
|
let parse_grid start_anywhere =
|
||||||
let s = ref @@ Vec2.of_tuple (0, 0) in
|
let s = ref @@ Vec2.origin in
|
||||||
let e = ref @@ Vec2.of_tuple (0, 0) in
|
let e = ref @@ Vec2.origin in
|
||||||
let unvisited = ref [] in
|
let unvisited = ref [] in
|
||||||
let grid =
|
let grid =
|
||||||
Utils.lines_of_input 12
|
Utils.lines_of_input 12
|
||||||
|> Vec2.parse_grid (fun char ->
|
|> Vec2.parse_grid (fun char -> { char; visited = false; distance = infinity })
|
||||||
{ char; visited = false; distance = infinity; previous = None })
|
|
||||||
in
|
in
|
||||||
Vec2.iter_grid grid (fun pos ->
|
Vec2.iter_grid grid (fun pos ->
|
||||||
let n = Vec2.at_e grid pos in
|
let n = Vec2.at_e grid pos in
|
||||||
@@ -45,16 +43,13 @@ let reachable f t =
|
|||||||
|
|
||||||
let rec path grid goal current unvisited =
|
let rec path grid goal current unvisited =
|
||||||
let current_node = Vec2.at_e grid current in
|
let current_node = Vec2.at_e grid current in
|
||||||
let neighbors = Vec2.directions |> List.map (fun dir -> Vec2.(current + dir)) in
|
Vec2.directions
|
||||||
neighbors
|
|> List.map (fun dir -> Vec2.(current + dir))
|
||||||
|> List.iter (fun neighbor ->
|
|> List.iter (fun neighbor ->
|
||||||
match Vec2.at grid neighbor with
|
match Vec2.at grid neighbor with
|
||||||
| Some node when (not node.visited) && reachable current_node.char node.char ->
|
| Some node when (not node.visited) && reachable current_node.char node.char ->
|
||||||
let new_distance = current_node.distance + 1 in
|
let new_distance = current_node.distance + 1 in
|
||||||
if new_distance < node.distance
|
if new_distance < node.distance then node.distance <- new_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
|
if Option.is_none @@ List.find_opt (fun x -> Vec2.(x = neighbor)) !unvisited
|
||||||
then unvisited := neighbor :: !unvisited
|
then unvisited := neighbor :: !unvisited
|
||||||
| _ -> ());
|
| _ -> ());
|
||||||
|
|||||||
+1
-1
@@ -37,7 +37,7 @@ let next_pos head tail =
|
|||||||
|
|
||||||
let solve length =
|
let solve length =
|
||||||
let moves = parse_input () in
|
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 tail = List.init (length - 1) (fun _ -> start) in
|
||||||
let _, _, last_knot_hist =
|
let _, _, last_knot_hist =
|
||||||
List.fold_left
|
List.fold_left
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ type 'a grid =
|
|||||||
}
|
}
|
||||||
[@@deriving show]
|
[@@deriving show]
|
||||||
|
|
||||||
|
let origin = 0, 0
|
||||||
let up = 0, 1
|
let up = 0, 1
|
||||||
let down = 0, -1
|
let down = 0, -1
|
||||||
let left = -1, 0
|
let left = -1, 0
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
type t [@@deriving show]
|
type t [@@deriving show]
|
||||||
type 'a grid [@@deriving show]
|
type 'a grid [@@deriving show]
|
||||||
|
|
||||||
|
val origin : t
|
||||||
val up : t
|
val up : t
|
||||||
val down : t
|
val down : t
|
||||||
val left : t
|
val left : t
|
||||||
|
|||||||
Reference in new issue
Block a user