1
0
Fork 0
advent-of-code/2020/02/part1.hs

36 lines
962 B
Haskell
Raw Normal View History

2022-09-21 09:19:52 -04:00
#!/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