1
0
Fork 0
advent-of-code/2020/04/part1.hs

49 lines
1.1 KiB
Haskell
Raw Normal View History

2022-09-21 09:19:52 -04:00
#!/usr/bin/env runghc
-- byr (Birth Year)
-- iyr (Issue Year)
-- eyr (Expiration Year)
-- hgt (Height)
-- hcl (Hair Color)
-- ecl (Eye Color)
-- pid (Passport ID)
-- cid (Country ID)
import Data.Map.Strict (fromList, member)
isValidEntry ("byr", _) = True
isValidEntry ("iyr", _) = True
isValidEntry ("eyr", _) = True
isValidEntry ("hgt", _) = True
isValidEntry ("hcl", _) = True
isValidEntry ("ecl", _) = True
isValidEntry ("pid", _) = True
isValidEntry ("cid", _) = True
isValidEntry _ = False
toEntry = fmap tail . break (==':')
main = interact (solve . getMaps)
solve = show . length . filter isValidDocument
isValidDocument document =
member "byr" document &&
member "iyr" document &&
member "eyr" document &&
member "hgt" document &&
member "hcl" document &&
member "ecl" document &&
member "pid" document
getMaps = map (fromList . filter isValidEntry . map toEntry . words) . joinLines . lines
joinLines = foldl parse [""]
where
parse [""] l = [l]
parse [a] "" = ["", a]
parse [a] l = [l ++ " " ++ a]
parse (a:as) l = parse [a] l ++ as