monotonic :: [Int] -> Bool monotonic xs = all (<0) xs || all (>0) xs gradual :: [Int] -> Bool gradual = all ((<= 3) . abs) differences :: [Int] -> [Int] differences (x:y:xs) = x-y : differences (y:xs) differences _ = [] safe :: [Int] -> Bool safe xs = let ys = differences xs in monotonic ys && gradual ys line :: String -> Bool line = safe . map read . words count :: (a -> Bool) -> [a] -> Int count p = foldr f 0 where f x y = if p x then succ y else y main :: IO () main = do s <- getContents let ls = lines s let xs = map line ls print $ count id xs