|
|
|
@ -2,18 +2,22 @@ |
|
|
|
|
|
|
|
|
|
module Day2 (day2) where |
|
|
|
|
|
|
|
|
|
import qualified Data.Text as T |
|
|
|
|
import qualified Data.Attoparsec.Text as P |
|
|
|
|
import Data.List (elemIndices) |
|
|
|
|
import qualified Data.Text as T |
|
|
|
|
|
|
|
|
|
type Max = Int |
|
|
|
|
|
|
|
|
|
type Min = Int |
|
|
|
|
|
|
|
|
|
type Letter = Char |
|
|
|
|
|
|
|
|
|
type Validator = Policy -> String -> Bool |
|
|
|
|
|
|
|
|
|
data Policy = Policy { lBound :: Min |
|
|
|
|
, uBound :: Max |
|
|
|
|
, letter :: Letter |
|
|
|
|
data Policy = Policy |
|
|
|
|
{ lBound :: Min, |
|
|
|
|
uBound :: Max, |
|
|
|
|
letter :: Letter |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parserPolicy :: P.Parser Policy |
|
|
|
@ -28,8 +32,8 @@ parserPolicy = do |
|
|
|
|
|
|
|
|
|
validatePolicy :: Validator |
|
|
|
|
validatePolicy p s = |
|
|
|
|
let l = length $ filter (== letter p) s in |
|
|
|
|
l >= lBound p && l <= uBound p |
|
|
|
|
let l = length $ filter (== letter p) s |
|
|
|
|
in l >= lBound p && l <= uBound p |
|
|
|
|
|
|
|
|
|
validateLine :: Validator -> String -> Bool |
|
|
|
|
validateLine v s = |
|
|
|
@ -46,14 +50,13 @@ validateCorrectPolicy :: Validator |
|
|
|
|
validateCorrectPolicy p s = |
|
|
|
|
let index1 = lBound p - 1 |
|
|
|
|
index2 = uBound p - 1 |
|
|
|
|
indices = elemIndices (letter p) s in |
|
|
|
|
xor (index1 `elem` indices) (index2 `elem` indices) |
|
|
|
|
|
|
|
|
|
indices = elemIndices (letter p) s |
|
|
|
|
in xor (index1 `elem` indices) (index2 `elem` indices) |
|
|
|
|
|
|
|
|
|
day2 :: IO () |
|
|
|
|
day2 = do |
|
|
|
|
rs <- lines <$> readFile "./input/day2" |
|
|
|
|
putStr "[Day 2-1] # valid passwords: " |
|
|
|
|
print . length . filter (==True) . fmap (validateLine validatePolicy) $ rs |
|
|
|
|
print . length . filter (== True) . fmap (validateLine validatePolicy) $ rs |
|
|
|
|
putStr "[Day 2-2] # valid passwords: " |
|
|
|
|
print . length . filter (==True) . fmap (validateLine validateCorrectPolicy) $ rs |
|
|
|
|
print . length . filter (== True) . fmap (validateLine validateCorrectPolicy) $ rs |
|
|
|
|