X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent15%2Fsrc%2Fadvent15.hs;h=4038d07538d17782dd0e2fb24a50718f68bc004b;hb=HEAD;hp=4aaee02bc5a2f9ab2ef09566dd2deb1bea699afe;hpb=7d3e83b7b1ed656c499a36f6faa5cfe83ca77656;p=advent-of-code-19.git diff --git a/advent15/src/advent15.hs b/advent15/src/advent15.hs index 4aaee02..4038d07 100644 --- a/advent15/src/advent15.hs +++ b/advent15/src/advent15.hs @@ -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