aoc2021/Day1_2.hs

19 lines
501 B
Haskell

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