parent
							
								
									55668d6d02
								
							
						
					
					
						commit
						38a9eb3c9f
					
				
				 4 changed files with 1087 additions and 1 deletions
			
			
		
									
										
											File diff suppressed because it is too large
											Load Diff
										
									
								
							
						@ -0,0 +1,75 @@ | 
				
			|||||||
 | 
					{-# LANGUAGE OverloadedStrings #-} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module Day4 (day4) where | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import Control.Applicative ((<|>)) | 
				
			||||||
 | 
					import Control.Monad (void) | 
				
			||||||
 | 
					import qualified Data.Attoparsec.Text as P | 
				
			||||||
 | 
					import qualified Data.Map as M | 
				
			||||||
 | 
					import Data.Maybe (catMaybes) | 
				
			||||||
 | 
					import qualified Data.Text as T | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type BYR = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type IYR = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type EYR = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type HGT = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type HCL = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ECL = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type PID = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type CID = T.Text | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					data Passport = Passport | 
				
			||||||
 | 
					  { byr :: BYR, | 
				
			||||||
 | 
					    iyr :: IYR, | 
				
			||||||
 | 
					    eyr :: EYR, | 
				
			||||||
 | 
					    hgt :: HGT, | 
				
			||||||
 | 
					    hcl :: HCL, | 
				
			||||||
 | 
					    ecl :: ECL, | 
				
			||||||
 | 
					    pid :: PID, | 
				
			||||||
 | 
					    cid :: Maybe CID -- Definitely not a hack! | 
				
			||||||
 | 
					  } | 
				
			||||||
 | 
					  deriving (Eq, Show) | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					parserPair :: P.Parser (T.Text, T.Text) | 
				
			||||||
 | 
					parserPair = do | 
				
			||||||
 | 
					  key <- P.count 3 P.letter | 
				
			||||||
 | 
					  _ <- P.char ':' | 
				
			||||||
 | 
					  value <- P.takeWhile1 (P.inClass "a-zA-Z0-9#") | 
				
			||||||
 | 
					  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 $ Passport 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 = do | 
				
			||||||
 | 
					  r <- readFile "./input/day4" | 
				
			||||||
 | 
					  putStr "[Day 4-1] # trees: " | 
				
			||||||
 | 
					  print . length . catMaybes . parseList $ r | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue