From 53ad5c4a8ce3367372acc3bc1e83e4ea68f54bb2 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Tue, 6 Dec 2022 10:56:10 +0530 Subject: [PATCH] day6 Signed-off-by: Amneesh Singh --- day6.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 day6.hs diff --git a/day6.hs b/day6.hs new file mode 100644 index 0000000..21207b5 --- /dev/null +++ b/day6.hs @@ -0,0 +1,25 @@ +import Data.List (nub) +import Text.ParserCombinators.ReadP + +main :: IO () +main = do + input <- readFile "day6.in" + putStr "Q1: " + print $ q1 input + putStr "Q2: " + print $ q2 input + + +q1, q2 :: String -> Int +q1 = length . parse 4 +q2 = length . parse 14 + +parse :: Int -> String -> String +parse n a = parse' n (splitAt n a) + +parse' :: Int -> (String, String) -> String +parse' n (a, b) + | length (nub (drop (length a - n) a)) == n = a + | otherwise = + let (c, cs) = last $ readP_to_S get b + in parse' n (a ++ [c], cs)