From e39685da36d625ce76f8ec376733dc67a80040d8 Mon Sep 17 00:00:00 2001 From: Crazazy Date: Mon, 2 Dec 2024 13:15:40 +0100 Subject: finished part 2 --- src/Day2.idr | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src') 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 -- cgit 1.4.1-2-gfad0