-
Notifications
You must be signed in to change notification settings - Fork 0
/
2b.hs
45 lines (37 loc) · 1.1 KB
/
2b.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
{-# LANGUAGE OverloadedStrings #-}
import Data.Either
import Text.ParserCombinators.Parsec
import Prelude
main = do
input <- getContents
putStr $ show $ fn $ lines input
safeIndex :: [a] -> Int -> Maybe a
safeIndex xs i
| (i > -1) && (length xs > i) = Just (xs !! i)
| otherwise = Nothing
charMatchAt :: Int -> Char -> [Char] -> Bool
charMatchAt x char str = case safeIndex str x of
Just y -> y == char
_ -> False
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) = case (charMatchAt (min - 1) char pass, charMatchAt (max - 1) char pass) of
(True, True) -> 0
(False, False) -> 0
_ -> 1
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 = foldl folder 0