diff options
Diffstat (limited to 'day02/day02.hs')
-rw-r--r-- | day02/day02.hs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/day02/day02.hs b/day02/day02.hs new file mode 100644 index 0000000..b263574 --- /dev/null +++ b/day02/day02.hs @@ -0,0 +1,47 @@ +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)) |