diff --git a/02/silver.hs b/02/silver.hs new file mode 100644 index 0000000..d12aa19 --- /dev/null +++ b/02/silver.hs @@ -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 diff --git a/02/test.txt b/02/test.txt new file mode 100644 index 0000000..b49c10d --- /dev/null +++ b/02/test.txt @@ -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