34 lines
874 B
Haskell
34 lines
874 B
Haskell
module Main (main) where
|
|
|
|
import Text.Printf (printf)
|
|
|
|
main :: IO ()
|
|
main = do
|
|
inputs <- readFile "./inputs/day2"
|
|
let columns = map (map (read :: String -> Int) . words) (lines inputs)
|
|
|
|
printf "safe: %d\n" $ length $ filter safe columns
|
|
|
|
safe :: (Ord a, Num a) => [a] -> Bool
|
|
safe x = distance x && ascOrDesc x
|
|
|
|
distance :: (Ord a, Num a) => [a] -> Bool
|
|
distance [] = True
|
|
distance [_] = True
|
|
distance (x : y : xs) = abs (x - y) <= 3 && abs (x - y) >= 1 && distance (y : xs)
|
|
|
|
ascOrDesc :: (Ord a, Num a) => [a] -> Bool
|
|
ascOrDesc [] = False
|
|
ascOrDesc [_] = False
|
|
ascOrDesc (x : y : xs) = (x < y && asc (x : y : xs)) || (x > y && desc (x : y : xs))
|
|
|
|
asc :: (Ord a, Num a) => [a] -> Bool
|
|
asc [] = True
|
|
asc [_] = True
|
|
asc (x : y : xs) = x < y && asc (y : xs)
|
|
|
|
desc :: (Ord a, Num a) => [a] -> Bool
|
|
desc [] = True
|
|
desc [_] = True
|
|
desc (x : y : xs) = x > y && desc (y : xs)
|