From a24758c516142d460d7302da070a2443cd713f29 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 12 Dec 2016 16:34:52 +0000 Subject: [PATCH] Updated readme --- README.html | 5 +++-- README.md | 3 ++- advent11.hs | 16 ++++++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.html b/README.html index 754e9be..7bffdaa 100644 --- a/README.html +++ b/README.html @@ -17,8 +17,9 @@

I'm using the basic Haskell Platform installation (install with

$ sudo aptitude install haskell-platform

).

-

I'm also using the MissingH library (install with

-
$ cabal install MissingH
+

I'm also using the MissingH and Parsec-number libraries (install with

+
$ cabal install MissingH
+$ cabal install parsec-number

)

Compile the code with

ghc --make advent01.hs
diff --git a/README.md b/README.md index 5c7f379..9fa5769 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,10 @@ $ sudo aptitude install haskell-platform ``` ). -I'm also using the `MissingH` library (install with +I'm also using the `MissingH` and `Parsec-number` libraries (install with ``` $ cabal install MissingH +$ cabal install parsec-number ``` ) diff --git a/advent11.hs b/advent11.hs index 5fd4e40..fd96636 100644 --- a/advent11.hs +++ b/advent11.hs @@ -44,12 +44,13 @@ buildingTest = Building 0 [ main :: IO () main = do part1 - part2 + -- part2 part1 :: IO () -- part1 = print $ length $ init $ extractJust $ hillClimb [[buildingTest]] [] -part1 = print $ length $ init $ extractJust $ hillClimb [[building1]] [] +-- 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 -- 2.34.1