aboutsummaryrefslogtreecommitdiff
path: root/2020/04-Passport_Processing/graou.hs
blob: 9993f29fdc310aba6cd3e6b40ab3e95c913f5238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- requires cabal install --lib megaparsec parser-combinators
module Main (main) where

import Control.Applicative.Permutations
import Control.Monad (void, when)
import Data.Maybe (catMaybes)
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)

exampleExpectedOutput = 2

data Passport = Passport { byr :: String
                         , iyr :: String
                         , eyr :: String
                         , hgt :: String
                         , hcl :: String
                         , ecl :: String
                         , pid :: String
                         } deriving (Show)

type Parser = Parsec Void String

parseString :: String -> Parser String
parseString key = do
  void $ string key
  void $ char ':'
  void (optional $ char '#')
  some alphaNumChar

spaces = char ' ' <|> char '\n'

parsePassport :: Parser (Maybe Passport)
parsePassport = do
  (byr, iyr, eyr, hgt, hcl, ecl, pid, cid) <- runPermutation $
    (,,,,,,,) <$> toPermutationWithDefault Nothing (Just <$> parseString "byr" <* spaces)
              <*> toPermutationWithDefault Nothing (Just <$> parseString "iyr" <* spaces)
              <*> toPermutationWithDefault Nothing (Just <$> parseString "eyr" <* spaces)
              <*> toPermutationWithDefault Nothing (Just <$> parseString "hgt" <* spaces)
              <*> toPermutationWithDefault Nothing (Just <$> parseString "hcl" <* spaces)
              <*> toPermutationWithDefault Nothing (Just <$> parseString "ecl" <* spaces)
              <*> toPermutationWithDefault Nothing (Just <$> parseString "pid" <* spaces)
              <*> toPermutationWithDefault Nothing (Just <$> parseString "cid" <* spaces)
  void (char '\n')
  return $ makePassport byr iyr eyr hgt hcl ecl pid cid

makePassport :: Maybe String -> Maybe String -> Maybe String -> Maybe String -> Maybe String -> Maybe String -> Maybe String -> Maybe String -> Maybe Passport
makePassport (Just byr) ( Just iyr) (Just eyr) (Just hgt) (Just hcl) (Just ecl) (Just pid) _ = Just $ Passport { byr=byr, iyr=iyr, eyr=eyr, hgt=hgt, hcl=hcl, ecl=ecl, pid=pid }
makePassport _ _ _ _ _ _ _ _ = Nothing

parsePassports :: Parser [Passport]
parsePassports = do
  passports <- some (parsePassport)
  eof
  return $ catMaybes passports

parseInput :: String -> IO [Passport]
parseInput filename = do
  input <- readFile filename
  case runParser parsePassports filename input of
    Left bundle -> die $ errorBundlePretty bundle
    Right passports -> return passports

compute :: [Passport]-> Int
compute passports = length passports

main :: IO ()
main = do
  example <- parseInput "example"
  let exampleOutput = compute example
  when  (exampleOutput /= exampleExpectedOutput)  (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
  input <- parseInput "input"
  print $ compute input