commit 948afa564b667d2b7752658576caf034967edcff Author: Agatha Lovelace Date: Thu Dec 2 15:44:17 2021 +0200 Late Day 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ddedf9e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +inputs/* \ No newline at end of file diff --git a/Day1.hs b/Day1.hs new file mode 100644 index 0000000..504cb08 --- /dev/null +++ b/Day1.hs @@ -0,0 +1,19 @@ +module Day1 where +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) + +strToInts :: String -> [Int] +strToInts = map read . lines + +depth :: [Int] -> Int +depth [] = 0 +depth [x] = 0 +depth (x:xs) + | head xs > x = 1 + depth xs + | otherwise = depth xs + +main :: IO () +main = do + input <- openFile "inputs/1.txt" ReadMode + contents <- hGetContents input + print $ depth $ strToInts contents + hClose input \ No newline at end of file diff --git a/Day1_2.hs b/Day1_2.hs new file mode 100644 index 0000000..1d0e975 --- /dev/null +++ b/Day1_2.hs @@ -0,0 +1,19 @@ +module Day1_2 where +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) + +strToInts :: String -> [Int] +strToInts = map read . lines + +depth :: [Int] -> Int +depth a@(x:y:z:xs) = case length a > 3 of + True + | sum [y,z,head xs] > sum [x,y,z] -> 1 + depth (y:z:xs) + | otherwise -> depth (y:z:xs) + False -> 0 + +main :: IO () +main = do + input <- openFile "inputs/1.txt" ReadMode + contents <- hGetContents input + print $ depth $ strToInts contents + hClose input \ No newline at end of file