From: Neil Smith Date: Tue, 5 Dec 2017 11:20:38 +0000 (+0000) Subject: Simplified day 4 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=e3253beb006544daee146dd54f71b301890adaeb;p=advent-of-code-17.git Simplified day 4 --- diff --git a/src/advent04/advent04.hs b/src/advent04/advent04.hs index b8ae85c..85408d8 100644 --- a/src/advent04/advent04.hs +++ b/src/advent04/advent04.hs @@ -1,11 +1,9 @@ -import Data.List (sort) -import Data.List.Split (splitOn) -import qualified Data.Map.Strict as M +import Data.List (sort, nub) main :: IO () main = do text <- readFile "data/advent04.txt" - let passphrases = map parseLine $ lines text + let passphrases = map words $ lines text print $ part1 passphrases print $ part2 passphrases @@ -15,14 +13,8 @@ part1 = length . filter (not . containsDuplicates) part2 :: [[String]] -> Int part2 = length . filter (not . containsAnagrams) -parseLine :: String -> [String] -parseLine = filter (not . null) . splitOn " " - -frequency :: (Ord a) => [a] -> M.Map a Int -frequency xs = M.fromListWith (+) [(x, 1) | x <- xs] - containsDuplicates :: [String] -> Bool -containsDuplicates = (not . M.null) . M.filter (> 1) . frequency +containsDuplicates passphrase = (length passphrase) /= (length $ nub passphrase) containsAnagrams :: [String] -> Bool containsAnagrams = containsDuplicates . (map sort)