1 import Data.List ((\\), nub, sortOn)
2 import Data.Bits (popCount)
3 import Data.Maybe (fromMaybe)
18 part1 = print $ length $ tail $ fromMaybe [] $ aStar [[(1, 1)]] []
21 part2 = do print $ length $ tail $ edl 50 [[(1, 1)]] []
22 putStrLn $ showRoomR 30 25 $ edl 50 [[(1, 1)]] []
25 -- extractJust :: Maybe [a] -> [a]
26 -- extractJust Nothing = []
27 -- extractJust (Just x) = x
29 isWall :: Int -> Int -> Bool
30 isWall x y = odd $ popCount n
32 n = x*x + 3*x + 2*x*y + y + y*y + seed
35 showRoom w h = showRoomR w h []
37 showRoomR w h reached = unlines rows
39 rows = [row x | x <- [0..h]]
40 row x = [showCell x y | y <- [0..w]]
41 showCell x y = if (isWall x y)
43 else if (x, y) `elem` reached
48 aStar :: [[Pos]] -> [Pos] -> Maybe [Pos]
50 aStar (currentTrail:trails) closed =
51 if isGoal (head currentTrail) then Just currentTrail
52 else if (head currentTrail) `elem` closed then aStar trails closed
53 else aStar newAgenda ((head currentTrail): closed)
55 sortOn (\a -> trailCost a) $
56 trails ++ (candidates currentTrail closed)
57 trailCost t = estimateCost (head t) + length t - 1
60 -- exhaustive depth-limited
61 edl :: Int -> [[Pos]] -> [Pos] -> [Pos]
62 edl _ [] closed = nub closed
63 edl limit (currentTrail:trails) closed =
64 if (length currentTrail) > (limit+1) then edl limit trails ((head currentTrail):closed)
65 else if (head currentTrail) `elem` closed then edl limit trails closed
66 else edl limit newAgenda ((head currentTrail):closed)
67 where newAgenda = trails ++ (candidates currentTrail closed)
69 candidates :: [Pos] -> [Pos] -> [[Pos]]
70 candidates currentTrail closed = newCandidates
72 (candidate:trail) = currentTrail
73 succs = legalSuccessors $ successors candidate
74 nonloops = (succs \\ trail) \\ closed
75 newCandidates = map (\n -> n:candidate:trail) nonloops
80 isLegal :: Pos -> Bool
82 x >= 0 && y >= 0 && (not $ isWall x y)
84 successors :: Pos -> [Pos]
85 successors (x, y) = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]
87 legalSuccessors :: [Pos] -> [Pos]
88 legalSuccessors = filter (isLegal)
90 estimateCost :: Pos -> Int
91 estimateCost (x, y) = abs (x - gx) + abs (y - gy)
92 where (gx, gy) = goal1