Day 2 silver star
This commit is contained in:
20
02/silver.hs
Normal file
20
02/silver.hs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
import Debug.Trace
|
||||||
|
|
||||||
|
debug = flip trace
|
||||||
|
|
||||||
|
isSortedGradually :: (Integer -> Integer -> Bool) -> [Integer] -> Bool
|
||||||
|
isSortedGradually f (x1:x2:xs) = (f x1 x2) && diff >= 1 && diff <= 3 && isSortedGradually f (x2:xs)
|
||||||
|
where diff = abs (x1 - x2)
|
||||||
|
isSortedGradually f (x1:_) = True
|
||||||
|
isSortedGradually f _ = True
|
||||||
|
|
||||||
|
isAscending :: [Integer] -> Bool
|
||||||
|
isAscending = isSortedGradually (<)
|
||||||
|
isDescending :: [Integer] -> Bool
|
||||||
|
isDescending = isSortedGradually (>)
|
||||||
|
|
||||||
|
ok xs = isAscending xs || isDescending xs
|
||||||
|
|
||||||
|
main :: IO()
|
||||||
|
main = interact $ show . sum . map (fromEnum . ok . map (read :: (String -> Integer)) . words) . lines
|
||||||
6
02/test.txt
Normal file
6
02/test.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
7 6 4 2 1
|
||||||
|
1 2 7 8 9
|
||||||
|
9 7 6 2 1
|
||||||
|
1 3 2 4 5
|
||||||
|
8 6 4 4 1
|
||||||
|
1 3 6 7 9
|
||||||
Reference in New Issue
Block a user