diff --git a/day6/main.hs b/day6/main.hs index 4824206..4b023a8 100755 --- a/day6/main.hs +++ b/day6/main.hs @@ -5,6 +5,65 @@ {-# LANGUAGE OverloadedStrings #-} +-- import Debug.Trace (trace) +import Data.List (nub) + +exampleData :: [String] +exampleData = + [ "abc" + , "" + , "a" + , "b" + , "c" + , "" + , "ab" + , "ac" + , "" + , "a" + , "a" + , "a" + , "a" + , "" + , "b" + ] + +-- Count how many different chars per paragraph and sum them +countUniqueAnswers :: [String] -> Int +countUniqueAnswers = length . nub . concat + +paragraphs :: [String] -> [[String]] +paragraphs strs = go (strs, []) + where + go :: ([String], [[String]]) -> [[String]] + go ([], ys) = ys + go (x:xs, []) = go (xs, [x] : []) + go ("":xs, ys) = go (xs, [] : ys) + go (x:xs, y:ys) = go (xs, (x:y) : ys) + +solvePart1 :: [String] -> Int +solvePart1 = sum . (map countUniqueAnswers) . paragraphs + +allAnsweredX :: Char -> [[String]] -> Bool +allAnsweredX c = all (any (== c)) + +solvePart2 :: [String] -> [Bool] +solvePart2 = allAnsweredX 'a' . paragraphs + main :: IO () main = do putStrLn "Day 6 - Part 1" + print $ exampleData + print $ countUniqueAnswers [""] + print $ countUniqueAnswers ["abc"] + print $ countUniqueAnswers ["a", "b", "c"] + print $ countUniqueAnswers ["ab", "ac"] + putStrLn "Day 6 - Part 1 : solving test" + print $ solvePart1 exampleData + putStrLn "Day 6 - Part 1 : solving input" + input <- lines <$> readFile "day6/input" + print $ solvePart1 input + putStrLn "Day 6 - Part 2 : solving test" + print $ solvePart2 exampleData + -- putStrLn "Day 6 - Part 2 : solving input" + -- print $ solvePart2 input +