summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-01-07 11:02:27 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-01-07 11:02:27 -0800
commit30ed87d9e949a24ddd481f8912e206def3ab2572 (patch)
tree79f1297779dd15ec862dfdc727c28f67c1cd86f1
parent6701539057f5e6790f46d602d0bfe7a7cb1317b3 (diff)
downloadAdventOfCode2016-30ed87d9e949a24ddd481f8912e206def3ab2572.tar.gz
use nuget for A* search instead of copypasta
-rw-r--r--day13.fsx48
1 files changed, 2 insertions, 46 deletions
diff --git a/day13.fsx b/day13.fsx
index a20a14a..d84e707 100644
--- a/day13.fsx
+++ b/day13.fsx
@@ -1,49 +1,5 @@
-type Config<'a> = 
-    {
-        neighbours: 'a -> seq<'a>
-        gCost: 'a -> 'a -> float
-        fCost: 'a -> 'a -> float
-        maxIterations: int option
-    }
-
-let search<'a when 'a : comparison> start goal config : seq<'a> option =
-
-    let rec reconstructPath cameFrom current =
-        seq {
-            yield current
-            match Map.tryFind current cameFrom with
-            | None -> ()
-            | Some next -> yield! reconstructPath cameFrom next
-        }
-
-    let rec crawler closedSet (openSet, gScores, fScores, cameFrom) =
-        match config.maxIterations with 
-        | Some n when n = Set.count closedSet -> None
-        | _ ->
-            match List.sortBy (fun n -> Map.find n fScores) openSet with
-            | current::_ when current = goal -> Some <| reconstructPath cameFrom current 
-            | current::rest ->
-                let gScore = Map.find current gScores
-                let next =
-                    config.neighbours current 
-                    |> Seq.filter (fun n -> closedSet |> Set.contains n |> not)
-                    |> Seq.fold (fun (openSet, gScores, fScores, cameFrom) neighbour ->
-                        let tentativeGScore = gScore + config.gCost current neighbour
-                        if List.contains neighbour openSet && tentativeGScore >= Map.find neighbour gScores 
-                        then (openSet, gScores, fScores, cameFrom)
-                        else
-                            let newOpenSet = if List.contains neighbour openSet then openSet else neighbour::openSet
-                            let newGScores = Map.add neighbour tentativeGScore gScores
-                            let newFScores = Map.add neighbour (tentativeGScore + config.fCost neighbour goal) fScores
-                            let newCameFrom = Map.add neighbour current cameFrom
-                            newOpenSet, newGScores, newFScores, newCameFrom
-                        ) (rest, gScores, fScores, cameFrom)
-                crawler (Set.add current closedSet) next
-            | _ -> None
-
-    let gScores = Map.ofList [start, 0.]
-    let fScores = Map.ofList [start, config.fCost start goal]
-    crawler Set.empty ([start], gScores, fScores, Map.empty)
+#r "nuget: astar-search, 1.0.2"
+open AStar
 
 let isWall (x, y) =
     let bits = System.Convert.ToString(x*x + 3*x + 2*x*y + y + y*y + 1350, 2)