Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.Char (ord)
- import qualified Data.Set as S
- import qualified Data.Vector as V
- type Coord = (Int,Int)
- type Done = S.Set Coord
- type Todo = [Coord]
- main = putStrLn $ show n
- n = loop 0 [(0,0)] S.empty
- loop :: Int -> Todo -> Done -> Int
- loop n [] _ = n
- loop n (t:ts) d = loop (n + count t) (also t d ++ ts) (S.insert t d)
- count :: Coord -> Int
- count (0,0) = 1
- count (0,_) = 4
- count (_,0) = 4
- count (x,y) = if x == y then 4 else 8
- also :: Coord -> Done -> Todo
- also c d = filter (\k -> S.notMember k d) $
- filter isAccessible $
- filter inQuadrant $
- next c
- isAccessible :: Coord -> Bool
- isAccessible (x,y) = sumDigits <= 19
- where
- sumDigits = sums V.! x + sums V.! y
- inQuadrant :: Coord -> Bool
- inQuadrant (x,y) = y <= x
- next :: Coord -> Todo
- next (x,y) = [(x+1,y), (x,y+1)]
- sums :: V.Vector Int
- sums = V.generate 300 sum1
- where
- sum1 n = sum $ map val (show n)
- val x = ord x - ord '0'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement