X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent11.hs;h=33bfe3c7cc2e788c03cfdc65bc46984179a0494c;hb=f9f0bc22576835cad53961170a64e7ffa0824fd8;hp=5fd4e40b784658a12580906efbbb2cb5774d336f;hpb=3f76a5d104ff97424a479e2c21ecd39f024547d0;p=advent-of-code-16.git

diff --git a/advent11.hs b/advent11.hs
index 5fd4e40..33bfe3c 100644
--- a/advent11.hs
+++ b/advent11.hs
@@ -50,6 +50,7 @@ main = do
 part1 :: IO ()
 -- part1 = print $ length $ init $ extractJust $ hillClimb [[buildingTest]] []
 part1 = print $ length $ init $ extractJust $ hillClimb [[building1]] []
+-- part1 = print $ length $ init $ extractJust $ aStar [[building1]] []
 
 part2 :: IO ()
 part2 = print $ length $ init $ extractJust $ hillClimb [[building2]] []
@@ -68,6 +69,17 @@ hillClimb (currentTrail:trails) closed =
             sortBy (\t1 t2 -> (head t1) `compare` (head t2)) $ 
             trails ++ (candidates currentTrail closed)
 
+aStar :: [[Building]] -> [Building] -> Maybe [Building]
+aStar [] _ = Nothing
+aStar (currentTrail:trails) closed = 
+    if isGoal (head currentTrail) then Just currentTrail
+    else aStar newAgenda ((head currentTrail): closed) 
+    where newAgenda = 
+            sortBy (\t1 t2 -> (trailCost t1) `compare` (trailCost t2)) $ 
+            trails ++ (candidates currentTrail closed)
+          trailCost t = estimateCost (head t) + length t - 1
+
+
 candidates :: [Building] -> [Building] -> [[Building]]
 candidates currentTrail closed = newCandidates
     where