Solve day 2, part 1

pull/1/head
Gaël Depreeuw 4 years ago
parent 5335e6fb8d
commit bc0a56688d
Signed by: Mithror
GPG Key ID: 8AB218ABA4867F78
  1. 2
      Main.hs
  2. 1000
      input/day2
  3. 43
      src/Day2.hs

@ -1,7 +1,9 @@
module Main where
import Day1 (day1)
import Day2 (day2)
main :: IO ()
main = do
day1
day2

File diff suppressed because it is too large Load Diff

@ -0,0 +1,43 @@
{-# LANGUAGE OverloadedStrings #-}
module Day2 (day2) where
import qualified Data.Text as T
import qualified Data.Attoparsec.Text as P
type Max = Int
type Min = Int
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 = 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 2-1] # valid passwords: "
print . length . filter (==True) . fmap validateLine . lines $ r
Loading…
Cancel
Save