-
Notifications
You must be signed in to change notification settings - Fork 0
/
2a.hs
36 lines (30 loc) · 873 Bytes
/
2a.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
{-# LANGUAGE OverloadedStrings #-}
import Data.Either
import Text.ParserCombinators.Parsec
import Prelude
main = do
input <- getContents
putStr $ show $ fn $ lines input
passwordParser :: GenParser Char st (Int, Int, Char, String)
passwordParser = do
min <- many1 digit
char '-'
max <- many1 digit
spaces
character <- letter
char ':'
spaces
password <- many1 letter
return (read min, read max, character, password)
passwordChecker :: (Int, Int, Char, String) -> Int
passwordChecker (min, max, char, pass)
| min <= occ && occ <= max = 1
| otherwise = 0
where
occ = foldl (\y x -> if x == char then y + 1 else y) 0 pass
folder :: Int -> String -> Int
folder acc xs = case parse passwordParser "Some Error" xs of
Left _ -> acc
Right passwordData -> acc + (passwordChecker passwordData)
fn :: [String] -> Int
fn xs = foldl folder 0 xs