22
[aoc20.git] / 4 / one.icl
1 module one
2
3 import StdEnv
4 import Text
5 import Data.Maybe
6 import Data.List
7 import Regex
8
9 read :: *File -> [String]
10 read f
11 # (l, f) = freadline f
12 | l.[size l - 1] == '\0' = []
13 = [l % (0, size l - 2):read f]
14
15 group :: [(String, String)] [String] -> [[(String, String)]]
16 group acc [] = [acc]
17 group acc ["":xs] = [acc:group [] xs]
18 group acc [s:xs] = group (acc ++ [(k, v)\\[k,v]<-map (split ":") (split " " s)]) xs
19
20 valid :: [(String, String)] [(String, String)] -> Bool
21 valid req fs = all (\(f, p)->maybe False (not o isEmpty o match p) (lookup f fs)) req
22
23 Start w
24 # (io, w) = stdio w
25 # passports = group [] (read io)
26 = (one passports, two passports)
27
28 one = length o filter (valid [(n, ".*")\\n<-["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]])
29 two = length o filter (valid
30 [ ("byr", "^(19[2-9]\\d|200[0-2])$")
31 , ("iyr", "^20(1\\d|20)$")
32 , ("eyr", "^20(2\\d|30)$")
33 , ("hgt", "^(1([5-8]\\d|9[0-3])cm|(59|6\\d|7[0-6])in)$")
34 , ("hcl", "^#[0-9a-f]{6}$")
35 , ("ecl", "^(amb|blu|brn|gry|grn|hzl|oth)$")
36 , ("pid", "^\\d{9}$")
37 ])