module Day2 import Data.String parseRow : String -> List Int parseRow str = do num <- words str maybe [] pure (parseInteger num) parseInp : String -> List (List Int) parseInp inp = do numrow <- lines inp pure $ parseRow numrow isSafe : List Int -> Bool isSafe [] = False isSafe (x :: xs) = fst up || fst down where up = foldl (\(b, y) => \n => (b && y - n <= 3 && y - n > 0, n)) (True, x) xs down = foldl (\(b, y) => \n => (b && n - y <= 3 && n - y > 0, n)) (True, x) xs btoi : Bool -> Int btoi False = 0 btoi True = 1 part1 : List (List Int) -> Int part1 iss = sum $ map (btoi . isSafe) iss export sol1 : String -> String sol1 = show . part1 . parseInp joinLists : (List a, List a) -> List a joinLists (x, []) = [] joinLists (x, (y :: xs)) = x ++ xs subLists : List a -> List (List a) subLists xs = do splitNum <- [0.. (length xs)] pure $ joinLists $ splitAt splitNum xs isSafe' : List Int -> Bool isSafe' xs = isSafe xs || (any isSafe $ subLists xs) part2 : List (List Int) -> Int part2 iss = sum $ map (btoi . isSafe') iss export sol2 : String -> String sol2 = show . part2 . parseInp export out : String -> String out = show . parseInp