import Data.Char (ord)
import qualified Data.Map as M
type Coord = (Int,Int)
type Done = M.Map Coord Bool
type Todo = [Coord]
main = putStrLn $ show n
n = loop 0 [(0,0)] M.empty
loop :: Int -> Todo -> Done -> Int
loop n [] _ = n
loop n (t:ts) d = loop (n + count t) (also t d ++ ts) (M.insert t True 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 -> M.notMember k d) $
filter isAccessible $
filter inQuadrant $
next c
isAccessible :: Coord -> Bool
isAccessible (x,y) = sumDigits <= 19
where
sumDigits = sum1 x + sum1 y
sum1 n = sum $ map val (show n)
val x = ord x - ord '0'
inQuadrant :: Coord -> Bool
inQuadrant (x,y) = y <= x
next :: Coord -> Todo
next (x,y) = [(x+1,y), (x,y+1)]