This commit is contained in:
Martin Potier 2020-12-07 16:54:34 +02:00
parent f7d651ff92
commit b344686716
No known key found for this signature in database
GPG key ID: D4DD957DBA4AD89E

View file

@ -5,6 +5,65 @@
{-# LANGUAGE OverloadedStrings #-} {-# 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 :: IO ()
main = do main = do
putStrLn "Day 6 - Part 1" 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