diff options
-rw-r--r-- | src/Day2.idr | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/Day2.idr b/src/Day2.idr index c3b26aa..34c0763 100644 --- a/src/Day2.idr +++ b/src/Day2.idr @@ -29,22 +29,20 @@ part1 iss = sum $ map (btoi . isSafe) iss export sol1 : String -> String sol1 = show . part1 . parseInp -upCheck, downCheck : Int -> Int -> Bool -upCheck i j = i - j <= 3 && i - j > 0 -downCheck i j = j - i <= 3 && j - i > 0 +joinLists : (List a, List a) -> List a +joinLists (x, []) = [] +joinLists (x, (y :: xs)) = x ++ xs -safeTerm : (Int -> Int -> Bool) -> (Bool, Bool, Int) -> Int -> (Bool, Bool, Int) -safeTerm f (False, (y, z)) i = (False, y, i) -safeTerm f (True, (False, z)) i = (f z i, False, i) -safeTerm f (True, (True, z)) i = if f z i then (True, True, i) else (True, False, z) +subLists : List a -> List (List a) +subLists xs = do + splitNum <- [0.. (length xs)] + pure $ joinLists $ splitAt splitNum xs -isSafe' : Bool -> List Int -> Bool -isSafe' b [] = False -isSafe' b (x :: []) = True -isSafe' b (x :: (y :: xs)) = isSafe (x :: y :: xs) || (b && isSafe' False (y :: xs)) || (b && isSafe' False (x :: xs)) +isSafe' : List Int -> Bool +isSafe' xs = isSafe xs || (any isSafe $ subLists xs) part2 : List (List Int) -> Int -part2 iss = sum $ map (btoi . isSafe' True) iss +part2 iss = sum $ map (btoi . isSafe') iss export sol2 : String -> String sol2 = show . part2 . parseInp |