-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.hs
38 lines (28 loc) · 1.14 KB
/
day2.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
import Text.Regex.Posix
thePattern :: String
thePattern = "([0-9]+)-([0-9]+) (.): (.*)"
parseTxt :: String -> (Int,Int,Char,String)
parseTxt x = let a = x =~ thePattern :: [[String]]
in (
read . (!!1) . head $ a,
read . (!!2) . head $ a,
head . (!!3) . head $ a,
(!!4) . head $ a
)
instCt :: Char -> String -> Int
instCt c = length . (filter (==c))
xor :: Bool -> Bool -> Bool
a `xor` b = (a && (not b)) || (b && (not a))
validPasswd :: (Int,Int,Char,String) -> Bool
validPasswd (a,b,c,s) = let x = instCt c s
in x >= a && x <= b
validPasswd2 :: (Int,Int,Char,String) -> Bool
validPasswd2 (a,b,c,s) = let x = s!!(a-1)
y = s!!(b-1)
in (x==c) `xor` (y==c)
problem1 :: IO ()
problem1 = readFile "day2.txt" >>= (putStrLn . show . length . (filter validPasswd) . (map parseTxt) . lines)
problem2 :: IO ()
problem2 = readFile "day2.txt" >>= (putStrLn . show . length . (filter validPasswd2) . (map parseTxt) . lines)
main :: IO ()
main = problem1 >> problem2