blob: 0327f1e5e0f7dcebec88ca94923eea9fd772c625 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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
safe' :: [Int] -> Bool
safe' = any safe . removals
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
remove :: Int -> [a] -> [a]
remove n xs = take n xs ++ drop (n+1) xs
removals :: [a] -> [[a]]
removals xs = map (flip remove xs) [0..length xs - 1]
main :: IO ()
main = do
s <- getContents
let ls = lines s
let xs = map line ls
print $ count id xs
|