Compare commits
No commits in common. '07084c40cdd0b070799374b18d8e7a3f53dea3f0' and '03759d2e794b6bc82fa8acebb48188908b01819b' have entirely different histories.
07084c40cd
...
03759d2e79
5 changed files with 12 additions and 1058 deletions
File diff suppressed because it is too large
Load Diff
@ -1,43 +0,0 @@ |
||||
{-# LANGUAGE OverloadedStrings #-} |
||||
|
||||
module Day2 (day2) where |
||||
|
||||
import qualified Data.Text as T |
||||
import qualified Data.Attoparsec.Text as P |
||||
|
||||
type Max = Integer |
||||
type Min = Integer |
||||
type Letter = Char |
||||
|
||||
data Policy = Policy { lBound :: Min |
||||
, uBound :: Max |
||||
, letter :: Letter |
||||
} |
||||
|
||||
parserPolicy :: P.Parser Policy |
||||
parserPolicy = do |
||||
lB <- P.decimal |
||||
_ <- P.char '-' |
||||
uB <- P.decimal |
||||
_ <- P.char ' ' |
||||
le <- P.letter |
||||
_ <- P.string ": " |
||||
return $ Policy lB uB le |
||||
|
||||
validatePolicy :: Policy -> String -> Bool |
||||
validatePolicy p s = |
||||
let l = toInteger . length $ filter (== letter p) s in |
||||
l >= lBound p && l <= uBound p |
||||
|
||||
validateLine :: String -> Bool |
||||
validateLine s = |
||||
case P.parse parserPolicy (T.pack s) of |
||||
P.Fail {} -> error "day2 - parser error" |
||||
P.Partial _ -> error "day2 - input error" |
||||
P.Done i r -> validatePolicy r (T.unpack i) |
||||
|
||||
day2 :: IO () |
||||
day2 = do |
||||
r <- readFile "./input/day2" |
||||
putStr "[Day 1-1] # valid passwords: " |
||||
print . length . filter (==True) . fmap validateLine . lines $ r |
Loading…
Reference in new issue