From 30ed87d9e949a24ddd481f8912e206def3ab2572 Mon Sep 17 00:00:00 2001 From: Brian Chu Date: Fri, 7 Jan 2022 11:02:27 -0800 Subject: use nuget for A* search instead of copypasta --- day13.fsx | 48 ++---------------------------------------------- 1 file 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) -- cgit 1.4.1-2-gfad0