adventofcode/2022/app/Day6/Main.hs
2022-12-08 22:09:56 +01:00

36 lines
791 B
Haskell

module Day6.Main (main) where
import System.IO
import Data.List
main :: IO ()
main = do
putStrLn "Day 6"
handle <- openFile "app/Day6/input" ReadMode
contents <- hGetContents handle
let r1 = part1 contents
putStrLn $ "part 1: " ++ show r1
let r2 = part2 contents
putStrLn $ "part 2: " ++ show r2
distinct xs = length (nub xs) == length xs
part1 :: String -> Int
part1 contents = go contents 0
where
go [] _ = 0
go (a:b:c:d:es) n | distinct [a,b,c,d] = n + 4
go (a:es) n = go es (n + 1)
part2 :: String -> Int
part2 contents = go contents 0
where
go [] _ = 0
go (e:es) n = if distinct (e : take 13 es)
then n + 14
else go es (n + 1)