Tidying.
[advent-of-code-19.git] / advent15 / src / advent15.hs
index 4aaee02bc5a2f9ab2ef09566dd2deb1bea699afe..4038d07538d17782dd0e2fb24a50718f68bc004b 100644 (file)
@@ -46,7 +46,7 @@ main = do
         print $ part2 mem
 
 part1 mem = _fromStart $ snd $ M.findMin $ M.filter (containsGoal) hull
-    where hull = fst $ head $ searchHull $ initialHullBoundary mem
+    where hull = searchHull $ initialHullBoundary mem
 
 
 part2 mem = fillTime hull S.empty [(start, 0)] 0
@@ -85,24 +85,14 @@ initialHullBoundary mem = (hull, [(0, 0)])
           hull = M.singleton (0, 0) (Empty {_droid = droid, _fromStart = 0, _isGoal = False})
 
 
-searchHull :: (Hull, Boundary) -> [(Hull, Boundary)]
-searchHull hullBoundary = dropWhile goalNotFound $ iterate searchHullStep hullBoundary
+searchHull :: (Hull, Boundary) -> Hull
+searchHull hullBoundary = fst $ head $ dropWhile goalNotFound $ iterate searchHullStep hullBoundary
 
 
 completeHull :: (Hull, Boundary) -> Hull
 completeHull hullBoundary = fst $ head $ dropWhile incomplete $ iterate searchHullStep hullBoundary
 
 
-fillTime _ _ [] t = t
-fillTime hull closed ((here, t):boundary) maxt
-    | hull!here == Wall = fillTime hull closed boundary maxt
-    | S.member here closed = fillTime hull closed boundary maxt
-    | otherwise = fillTime hull closed' (boundary ++ neighbours) (max maxt t)
-    where closed' = S.insert here closed
-          neighbours = map (\d -> (step here d, t + 1)) directions
-          directions = [North, East, South, West] :: [Direction]
-
-
 searchHullStep :: (Hull, Boundary) -> (Hull, Boundary)
 -- searchHullStep (hull, _) | trace (showHull hull) False = undefined
 searchHullStep (hull, []) = (hull, [])
@@ -123,6 +113,16 @@ searchHullDirection here (hull, boundary) direction
                           , _isGoal = (found == Goal)
                           }
 
+fillTime :: Hull -> (S.Set Position) -> [(Position, Integer)] -> Integer -> Integer
+fillTime _ _ [] t = t
+fillTime hull closed ((here, t):boundary) maxt
+    | hull!here == Wall = fillTime hull closed boundary maxt
+    | S.member here closed = fillTime hull closed boundary maxt
+    | otherwise = fillTime hull closed' (boundary ++ neighbours) (max maxt t)
+    where closed' = S.insert here closed
+          neighbours = map (\d -> (step here d, t + 1)) directions
+          directions = [North, East, South, West] :: [Direction]
+
 goalNotFound :: (Hull, Boundary) -> Bool
 goalNotFound (hull, _boundary) = M.null $ M.filter containsGoal hull