1 -- Writeup at https://work.njae.me.uk/2021/12/16/advent-of-code-2021-day-15/
5 -- import qualified Data.Text.IO as TIO
7 import qualified Data.PQueue.Prio.Min as P
8 import qualified Data.Set as S
9 import qualified Data.Sequence as Q
10 import Data.Sequence ((<|), (|>), (><)) --, ViewR( (:>) ), ViewL( (:<) ))
11 import Data.Foldable (foldl', sum) -- (toList, foldr', foldl', all)
13 import Control.Monad.Reader
14 import Control.Lens hiding ((<|), (|>), (:>), (:<))
15 import Data.Maybe (fromMaybe)
16 import Linear (V2(..), (^+^), (^-^), (*^), (^*))
17 import Data.Array.IArray
19 pattern Empty <- (Q.viewl -> Q.EmptyL) where Empty = Q.empty
20 pattern x :< xs <- (Q.viewl -> x Q.:< xs) where (:<) = (Q.<|)
21 pattern xs :> x <- (Q.viewr -> xs Q.:> x) where (:>) = (Q.|>)
23 type BasePosition = V2 Int -- r, c
24 newtype Position = Position BasePosition -- r, c
25 deriving (Eq, Ord, Show)
26 newtype TiledPosition = TiledPosition BasePosition -- r, c
27 deriving (Eq, Ord, Show)
28 type Grid = Array BasePosition Int
32 , _goal :: BasePosition
33 } deriving (Eq, Ord, Show)
36 type CaveContext = Reader Cave
40 Agendum { _current :: s
47 type Agenda s = P.MinPQueue Int (Agendum s)
49 type ExploredStates s = S.Set s
51 class (Eq s, Ord s, Show s) => SearchState s where
52 unwrapPos :: s -> BasePosition
53 successors :: s -> CaveContext (Q.Seq s)
54 estimateCost :: s -> CaveContext Int
56 isGoal :: s -> CaveContext Bool
57 entryCost :: s -> CaveContext Int
60 instance SearchState Position where
62 unwrapPos (Position p) = p
64 emptySearchState = Position (V2 0 0)
66 -- successors :: Position -> CaveContext (Q.Seq Position)
70 filter (inRange (bounds grid))
71 [ (unwrapPos here) ^+^ delta
72 | delta <- [V2 -1 0, V2 1 0, V2 0 -1, V2 0 1]
74 let succs = Q.fromList $ map Position neighbours
77 -- estimateCost :: Position -> CaveContext Int
80 let (V2 dr dc) = (unwrapPos here) ^-^ goal
81 return $ (abs dr) + (abs dc)
83 -- isGoal :: here -> CaveContext Bool
86 return $ (unwrapPos here) == goal
90 return $ grid ! (unwrapPos here)
92 instance SearchState TiledPosition where
94 emptySearchState = TiledPosition (V2 0 0)
96 unwrapPos (TiledPosition p) = p
98 -- successors :: Position -> CaveContext (Q.Seq Position)
99 successors (TiledPosition here) =
100 do grid <- asks _grid
101 let (lowBound, highBound) = bounds grid
102 let extendedBounds = ( lowBound
103 , tileScale highBound
106 filter (inRange extendedBounds)
108 | delta <- [V2 -1 0, V2 1 0, V2 0 -1, V2 0 1]
110 let succs = Q.fromList $ map TiledPosition neighbours
113 -- estimateCost :: Position -> CaveContext Int
114 estimateCost (TiledPosition here) =
115 do goal <- asks _goal
116 let (V2 dr dc) = here ^-^ (tileScale goal)
117 return $ (abs dr) + (abs dc)
119 -- isGoal :: here -> CaveContext Bool
120 isGoal (TiledPosition here) =
121 do goal <- asks _goal
122 return $ here == (tileScale goal)
124 entryCost (TiledPosition (V2 r c)) =
125 do grid <- asks _grid
126 let (_, V2 maxR maxC) = bounds grid
127 let (tileR, gridR) = r `divMod` (maxR + 1)
128 let (tileC, gridC) = c `divMod` (maxC + 1)
129 let gridCost = grid ! (V2 gridR gridC)
130 let !cost = (gridCost - 1 + tileR + tileC) `mod` 9 + 1
133 tileScale :: BasePosition -> BasePosition
134 tileScale (V2 r c) = V2 (ts r) (ts c)
135 where ts n = (n + 1) * 5 - 1
137 ------------------------------
141 do text <- readFile "data/advent15.txt"
142 let cave = mkCave text
145 -- print $ part2 grid
147 mkCave :: String -> Cave
148 mkCave text = Cave { _grid = grid, _goal = V2 r c }
149 where rows = lines text
151 c = (length $ head rows) - 1
152 grid = listArray ((V2 0 0), (V2 r c)) $ map mkCell $ concat rows
153 mkCell e = digitToInt e
157 part1 cave = maybe 0 _cost result
158 where result = runReader searchCave cave :: Maybe (Agendum Position)
161 part2 cave = maybe 0 _cost result
162 where result = runReader searchCave cave :: Maybe (Agendum TiledPosition)
165 searchCave :: SearchState s => CaveContext (Maybe (Agendum s))
167 do agenda <- initAgenda
170 initAgenda :: SearchState s => CaveContext (Agenda s)
172 do let ss = emptySearchState
174 return $ P.singleton c Agendum { _current = ss, _trail = Q.empty, _trailCost = 0, _cost = c}
177 aStar :: SearchState s => Agenda s -> ExploredStates s -> CaveContext (Maybe (Agendum s))
179 -- | trace ("Peeping " ++ (show $ fst $ P.findMin agenda) ++ ": " ++ (show reached) ++ " <- " ++ (show $ toList $ Q.take 1 $ _trail $ currentAgendum) ++ " :: " ++ (show newAgenda)) False = undefined
180 -- | trace ("Peeping " ++ (show $ _current $ snd $ P.findMin agenda) ) False = undefined
181 | P.null agenda = return Nothing
183 do let (_, currentAgendum) = P.findMin agenda
184 let reached = currentAgendum ^. current
185 nexts <- candidates currentAgendum closed
186 let newAgenda = foldl' (\q a -> P.insert (_cost a) a q) (P.deleteMin agenda) nexts
187 reachedGoal <- isGoal reached
189 then return (Just currentAgendum)
190 else if reached `S.member` closed
191 then aStar (P.deleteMin agenda) closed
192 else aStar newAgenda (S.insert reached closed)
195 candidates :: SearchState s => Agendum s -> ExploredStates s -> CaveContext (Q.Seq (Agendum s))
196 candidates agendum closed =
197 do let candidate = agendum ^. current
198 let previous = agendum ^. trail
199 let prevCost = agendum ^. trailCost
200 succs <- successors candidate
201 let nonloops = Q.filter (\s -> s `S.notMember` closed) succs
202 mapM (makeAgendum previous prevCost) nonloops
205 makeAgendum :: SearchState s => (Q.Seq s) -> Int -> s -> CaveContext (Agendum s)
206 makeAgendum previous prevCost newPosition =
207 do predicted <- estimateCost newPosition
209 let newTrail = previous |> newPosition
210 newPositionCost <- entryCost newPosition
211 let incurred = prevCost + newPositionCost
212 return Agendum { _current = newPosition
214 , _trailCost = incurred
215 , _cost = incurred + predicted