import Data.List import Data.List.Split import Data.Maybe data Dir = Up | Down safe :: [Int] -> Int -> Dir -> Bool safe [] _ _ = True safe (x:xs) last Up | x - last < 1 = False | x - last > 3 = False | otherwise = safe xs x Up safe (x:xs) last Down | last - x < 1 = False | last - x > 3 = False | otherwise = safe xs x Down isSafe (x:y:rest) | y > x = safe (y:rest) x Up | x > y = safe (y:rest) x Down | otherwise = False deleteAt x list = let (h, t) = splitAt x list in h ++ (drop 1 t) isSafeDamped Nothing rep | isSafe rep = True | otherwise = isSafeDamped (Just 0) rep isSafeDamped (Just x) rep | x >= length rep = False | isSafe $ deleteAt x rep = True | otherwise = isSafeDamped (Just (x + 1)) rep part1 reps = length $ filter (isSafe) reps part2 reps = length $ filter (isSafeDamped Nothing) reps main = do file <- readFile "day02.input" let lns = map (splitOn " ") $ lines file let reps = map (map (read::String->Int)) $ lns putStrLn ("Part 1: " ++ (show $ part1 reps)) putStrLn ("Part 2: " ++ (show $ part2 reps))