-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.hs
executable file
·48 lines (37 loc) · 1.08 KB
/
part1.hs
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
#!/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