Files
adventofcode2024/day2/app/Pt2.hs
Joey Eamigh b606826561 day3
2024-12-03 20:09:32 -05:00

46 lines
1.4 KiB
Haskell

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