Advertisement
rayl

Monkey Grid Puzzle

Aug 23rd, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Char (ord)
  2. import qualified Data.Map as M
  3.  
  4. type Coord = (Int,Int)
  5. type Done = M.Map Coord Bool
  6. type Todo = [Coord]
  7.  
  8. main = putStrLn $ show n
  9.  
  10. n = loop 0 [(0,0)] M.empty
  11.  
  12. loop :: Int -> Todo -> Done -> Int
  13. loop n [] _ = n
  14. loop n (t:ts) d = loop (n + count t) (also t d ++ ts) (M.insert t True d)
  15.  
  16. count :: Coord -> Int
  17. count (0,0) = 1
  18. count (0,_) = 4
  19. count (_,0) = 4
  20. count (x,y) = if x == y then 4 else 8
  21.  
  22. also :: Coord -> Done -> Todo
  23. also c d = filter (\k -> M.notMember k d) $
  24.            filter isAccessible $
  25.            filter inQuadrant $
  26.            next c
  27.  
  28. isAccessible :: Coord -> Bool
  29. isAccessible (x,y) = sumDigits <= 19
  30.   where
  31.     sumDigits = sum1 x + sum1 y
  32.     sum1 n = sum $ map val (show n)
  33.     val x = ord x - ord '0'
  34.  
  35. inQuadrant :: Coord -> Bool
  36. inQuadrant (x,y) = y <= x
  37.  
  38. next :: Coord -> Todo
  39. next (x,y) = [(x+1,y), (x,y+1)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement