X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent15%2FMain.hs;h=40a152a188969791c03f5dbc986ccc495b8b984f;hb=e2b15781f674220586e860fb9a85b6ad0f278fad;hp=227f16018acb46da0daf10746c6e642ed180a122;hpb=513138024a69ef9f5ade5031cd945567b6510689;p=advent-of-code-21.git diff --git a/advent15/Main.hs b/advent15/Main.hs index 227f160..40a152a 100644 --- a/advent15/Main.hs +++ b/advent15/Main.hs @@ -1,12 +1,9 @@ --- Writeup at https://work.njae.me.uk/2021/12/13/advent-of-code-2021-day-13/ - +-- Writeup at https://work.njae.me.uk/2021/12/16/advent-of-code-2021-day-15/ import Debug.Trace -- import qualified Data.Text.IO as TIO --- import qualified Data.Map.Strict as M --- import Data.Map.Strict ((!)) import qualified Data.PQueue.Prio.Min as P import qualified Data.Set as S import qualified Data.Sequence as Q @@ -18,8 +15,6 @@ import Control.Lens hiding ((<|), (|>), (:>), (:<)) import Data.Maybe (fromMaybe) import Linear (V2(..), (^+^), (^-^), (*^), (^*)) import Data.Array.IArray --- import Data.List - pattern Empty <- (Q.viewl -> Q.EmptyL) where Empty = Q.empty pattern x :< xs <- (Q.viewl -> x Q.:< xs) where (:<) = (Q.<|) @@ -40,7 +35,6 @@ makeLenses ''Cave type CaveContext = Reader Cave - data Agendum s = Agendum { _current :: s , _trail :: Q.Seq s @@ -55,9 +49,9 @@ type ExploredStates s = S.Set s class (Eq s, Ord s, Show s) => SearchState s where unwrapPos :: s -> BasePosition + emptySearchState :: s successors :: s -> CaveContext (Q.Seq s) estimateCost :: s -> CaveContext Int - emptySearchState :: s isGoal :: s -> CaveContext Bool entryCost :: s -> CaveContext Int @@ -69,37 +63,37 @@ instance SearchState Position where emptySearchState = Position (V2 0 0) -- successors :: Position -> CaveContext (Q.Seq Position) - successors here = + successors (Position here) = do grid <- asks _grid let neighbours = filter (inRange (bounds grid)) - [ (unwrapPos here) ^+^ delta + [ here ^+^ delta | delta <- [V2 -1 0, V2 1 0, V2 0 -1, V2 0 1] ] let succs = Q.fromList $ map Position neighbours return succs -- estimateCost :: Position -> CaveContext Int - estimateCost here = + estimateCost (Position here) = do goal <- asks _goal - let (V2 dr dc) = (unwrapPos here) ^-^ goal + let (V2 dr dc) = here ^-^ goal return $ (abs dr) + (abs dc) -- isGoal :: here -> CaveContext Bool - isGoal here = + isGoal (Position here) = do goal <- asks _goal - return $ (unwrapPos here) == goal + return $ here == goal - entryCost here = + entryCost (Position here) = do grid <- asks _grid - return $ grid ! (unwrapPos here) + return $ grid ! here instance SearchState TiledPosition where - emptySearchState = TiledPosition (V2 0 0) - unwrapPos (TiledPosition p) = p + emptySearchState = TiledPosition (V2 0 0) + -- successors :: Position -> CaveContext (Q.Seq Position) successors (TiledPosition here) = do grid <- asks _grid