diff options
author | Crazazy | 2024-12-02 12:30:22 +0100 |
---|---|---|
committer | Crazazy | 2024-12-02 12:30:22 +0100 |
commit | eec7eaf755d7cb57e85df84b4e6cb8f525bff5e7 (patch) | |
tree | 820f93e6934d2d08380c906ff60effa4bbf975f9 /src/Day2.idr | |
parent | 1e8ef84944374626a7670c7bd184b3d1ddf78046 (diff) |
added day 2
part 2 is still not working
Diffstat (limited to 'src/Day2.idr')
-rw-r--r-- | src/Day2.idr | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/Day2.idr b/src/Day2.idr new file mode 100644 index 0000000..c3b26aa --- /dev/null +++ b/src/Day2.idr @@ -0,0 +1,53 @@ +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 + +upCheck, downCheck : Int -> Int -> Bool +upCheck i j = i - j <= 3 && i - j > 0 +downCheck i j = j - i <= 3 && j - i > 0 + +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) + +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)) + +part2 : List (List Int) -> Int +part2 iss = sum $ map (btoi . isSafe' True) iss + +export sol2 : String -> String +sol2 = show . part2 . parseInp + +export out : String -> String +out = show . parseInp |