summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorCrazazy2024-12-02 13:15:40 +0100
committerCrazazy2024-12-02 13:15:40 +0100
commite39685da36d625ce76f8ec376733dc67a80040d8 (patch)
tree520c84d398ac8aff31891fbdfeb760bda193b44c /src
parenteec7eaf755d7cb57e85df84b4e6cb8f525bff5e7 (diff)
finished part 2
Diffstat (limited to 'src')
-rw-r--r--src/Day2.idr22
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