-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.hs
executable file
·35 lines (26 loc) · 962 Bytes
/
part1.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
#!/usr/bin/env runghc
import Data.Char
data PasswordEntry = PasswordEntry { min :: Int, max :: Int, character :: Char, password :: String }
deriving (Show)
-- This could be a lot better...
instance Read PasswordEntry where
readsPrec _ input =
let (min', rest) = span isDigit input
min = read min' :: Int
(_, rest') = splitAt 1 rest
(max', rest'') = span isDigit rest'
max = read max' :: Int
(_:char:_:_:password) = rest''
in
[(PasswordEntry min max char password, "")]
main = interact solve
solve = show . length . filter (isValidPasswordEntry . readPasswordEntry) . lines
readPasswordEntry :: String -> PasswordEntry
readPasswordEntry = read
isValidPasswordEntry (PasswordEntry min max char pass) =
let countedElem = countElem char pass
in min <= countedElem && max >= countedElem
countElem _ [] = 0
countElem a (x:xs)
| a == x = 1 + countElem a xs
countElem a (_:xs) = countElem a xs