diff --git a/haskellAoC/src/Y2015/Day05.hs b/haskellAoC/src/Y2015/Day05.hs new file mode 100644 index 0000000..28e8d83 --- /dev/null +++ b/haskellAoC/src/Y2015/Day05.hs @@ -0,0 +1,45 @@ +module Y2015.Day05 (y15day05) where + +has3vowels :: String -> Bool +has3vowels input = (>= 3) $ length $ filter (`elem` "aeiou") input + +hasArepeat :: String -> Bool +hasArepeat (x:x2:xs) + | x == x2 = True + | otherwise = hasArepeat (x2:xs) +hasArepeat _ = False + +noForbiddenStr :: String -> Bool +noForbiddenStr (x:x2:xs) + | (x:[x2]) `elem` ["ab", "cd", "pq", "xy"] = False + | otherwise = noForbiddenStr (x2:xs) +noForbiddenStr _ = True + +contains :: String -> String -> Bool +contains _ [] = True +contains [] _ = False +contains (x:xs) (y:ys) + | x == y = contains xs ys + | otherwise = contains xs (y:ys) + +repeatedTwoLetters :: String -> Bool +repeatedTwoLetters (x1:x2:xs) + | xs `contains` (x1:[x2]) = True + | otherwise = repeatedTwoLetters (x2:xs) +repeatedTwoLetters _ = False + +aRepeatAcrossOne :: String -> Bool +aRepeatAcrossOne (x1:x2:x3:xs) + | x1 == x3 = True + | otherwise = aRepeatAcrossOne (x2:x3:xs) +aRepeatAcrossOne _ = False + +y15day05 :: [String] -> (String, String) +y15day05 input = (part1, part2) + where + part1rules = [has3vowels, hasArepeat, noForbiddenStr] + part2rules = [repeatedTwoLetters, aRepeatAcrossOne] + applyRules rules pwd = all (== True) $ map ($ pwd) rules + countCorrectPwd rules = length $ filter (== True) $ map (applyRules rules) input + part1 = show $ countCorrectPwd part1rules + part2 = "WIP, invalid: " ++ (show $ countCorrectPwd part2rules) diff --git a/haskellAoC/src/Y2015/Days.hs b/haskellAoC/src/Y2015/Days.hs index e324e42..d3f4d18 100644 --- a/haskellAoC/src/Y2015/Days.hs +++ b/haskellAoC/src/Y2015/Days.hs @@ -4,10 +4,11 @@ import Y2015.Day01 import Y2015.Day02 import Y2015.Day03 import Y2015.Day04 - +import Y2015.Day05 year2015 :: String -> [String] -> (String, String) year2015 "01" = y15day01 year2015 "02" = y15day02 year2015 "03" = y15day03 year2015 "04" = y15day04 +year2015 "05" = y15day05