Compare commits

..

1 Commits

Author SHA1 Message Date
Gaël Depreeuw 47c1560549
Solve day 4, part 1 4 years ago
  1. 2
      Main.hs
  2. 68
      src/Day4.hs

@ -3,9 +3,11 @@ module Main where
import Day1 (day1) import Day1 (day1)
import Day2 (day2) import Day2 (day2)
import Day3 (day3) import Day3 (day3)
import Day4 (day4)
main :: IO () main :: IO ()
main = do main = do
day1 day1
day2 day2
day3 day3
day4

@ -5,28 +5,27 @@ module Day4 (day4) where
import Control.Applicative ((<|>)) import Control.Applicative ((<|>))
import Control.Monad (void) import Control.Monad (void)
import qualified Data.Attoparsec.Text as P import qualified Data.Attoparsec.Text as P
import qualified Data.Map as M
import Data.Maybe (catMaybes)
import qualified Data.Text as T
-- import qualified Data.Text as T type BYR = T.Text
-- import qualified Data.Vector as V
-- import Numeric.Natural (Natural)
type BYR = String type IYR = T.Text
type IYR = String type EYR = T.Text
type EYR = String type HGT = T.Text
type HGT = String type HCL = T.Text
type HCL = String type ECL = T.Text
type ECL = String type PID = T.Text
type PID = String type CID = T.Text
type CID = String data Passport = ValidPassport
data Passport = Passport
{ byr :: BYR, { byr :: BYR,
iyr :: IYR, iyr :: IYR,
eyr :: EYR, eyr :: EYR,
@ -38,18 +37,39 @@ data Passport = Passport
} }
deriving (Eq, Show) deriving (Eq, Show)
parsePassport :: P.Parser Passport parserPair :: P.Parser (T.Text, T.Text)
parsePassport = do parserPair = do
-- parse pairs
pairs <- parsePair `P.sepBy` (void P.space <|> P.endOfLine)
undefined
parsePair :: P.Parser (String, String)
parsePair = do
key <- P.count 3 P.letter key <- P.count 3 P.letter
_ <- P.char ':' _ <- P.char ':'
value <- P.many1 P.anyChar value <- P.takeWhile1 (P.inClass "a-zA-Z0-9#")
return (key, value) return (T.pack key, value)
parserEntry :: P.Parser (M.Map T.Text T.Text)
parserEntry = M.fromList <$> parserPair `P.sepBy` (void P.space <|> P.endOfLine)
fromMap :: M.Map T.Text T.Text -> Maybe Passport
fromMap m = do
byr <- m M.!? "byr"
iyr <- m M.!? "iyr"
eyr <- m M.!? "eyr"
hgt <- m M.!? "hgt"
hcl <- m M.!? "hcl"
ecl <- m M.!? "ecl"
pid <- m M.!? "pid"
return $ ValidPassport byr iyr eyr hgt hcl ecl pid (m M.!? "cid")
parserPassport :: P.Parser (Maybe Passport)
parserPassport = fromMap <$> parserEntry
parseList :: String -> [Maybe Passport]
parseList s =
let p = parserPassport `P.sepBy` P.endOfLine
in case P.parseOnly p (T.pack s) of
Left s -> error (show s)
Right r -> r
day4 :: IO () day4 :: IO ()
day4 = putStrLn "Day 4" day4 = do
r <- readFile "./input/day4"
putStr "[Day 4-1] # trees: "
print . length . catMaybes . parseList $ r
Loading…
Cancel
Save