-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaughtyAndNice.hs
45 lines (33 loc) · 1.33 KB
/
NaughtyAndNice.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module NaughtyAndNice where
import Data.List (isInfixOf)
countNiceStrings :: String -> Int
countNiceStrings input = length $ filter partTwoNiceString (lines input)
-- Part 1
niceString :: String -> Bool
niceString s = threeVowels s && twiceInRow s && noBadSubstrings s
vowels = "aoeui"
isVowel :: Char -> Bool
isVowel = (`elem` vowels) -- The backticks and parentheses essentially
-- swap the arguments
threeVowels :: String -> Bool
threeVowels s = length (filter isVowel s) >= 3
twiceInRow :: String -> Bool
twiceInRow (x:xs) = let pairs = zip (x:xs) xs
in any (uncurry (==)) pairs
twiceInRow _ = False
noBadSubstrings :: String -> Bool
noBadSubstrings s = let badInfix = ["ab", "cd", "pq", "xy"]
in not $ or $ map (`isInfixOf` s) badInfix
-- Part 2
partTwoNiceString :: String -> Bool
partTwoNiceString s = pairAppearsTwice s && repeatWithLetterBetween s
pairAppearsTwice :: String -> Bool
pairAppearsTwice (x:y:xs) = if [x, y] `isInfixOf` xs
then True
else pairAppearsTwice (y:xs)
pairAppearsTwice _ = False
repeatWithLetterBetween :: String -> Bool
repeatWithLetterBetween (x:y:xs) = let pairs = zip (x:y:xs) xs
in any (uncurry (==)) pairs
repeatWithLetterBetween _ =
False