summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorCrazazy2024-12-02 12:30:22 +0100
committerCrazazy2024-12-02 12:30:22 +0100
commiteec7eaf755d7cb57e85df84b4e6cb8f525bff5e7 (patch)
tree820f93e6934d2d08380c906ff60effa4bbf975f9 /src
parent1e8ef84944374626a7670c7bd184b3d1ddf78046 (diff)
added day 2
part 2 is still not working
Diffstat (limited to 'src')
-rw-r--r--src/Day2.idr53
-rw-r--r--src/Main.idr2
2 files changed, 55 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
diff --git a/src/Main.idr b/src/Main.idr
index e2faf59..c90397f 100644
--- a/src/Main.idr
+++ b/src/Main.idr
@@ -1,6 +1,7 @@
 module Main
 
 import Day1
+import Day2
 import System
 import System.File
 import Data.Fin
@@ -9,6 +10,7 @@ import Data.String
 runDay : Int -> String -> String
 runDay n inp = case n of
   1 => unlines [Day1.sol1 inp, Day1.sol2 inp]
+  2 => unlines [Day2.sol1 inp, Day2.sol2 inp]
   _ => "Not implemented yet!"
 
 main : IO ()