Late Day 1

This commit is contained in:
Agatha Lovelace 2021-12-02 15:44:17 +02:00
commit 948afa564b
3 changed files with 39 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
inputs/*

19
Day1.hs Normal file
View File

@ -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

19
Day1_2.hs Normal file
View File

@ -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