about summary refs log tree commit diff
path: root/day04.clj
blob: b33e8389fade37393329bc1569264eb5f025b9aa (plain) (blame)
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
(defn parse [s]
  (into {} (map #(vec (drop 1 %)) (re-seq #"(\w+):(\S+)" s))))

(def data
  (map parse (clojure.string/split (slurp "day04") #"\n\n")))

(defn valid-keys? [r]
  (empty? (clojure.set/difference
           #{"byr" "iyr" "eyr" "hgt" "hcl" "ecl" "pid"}
           (set (keys r)))))

(defn valid-height? [s]
  (if-let [[_ n u] (re-matches #"([0-9]+)(cm|in)" s)]
    (or (and (= u "cm") (<= 150 (Integer/parseInt n) 193))
        (and (= u "in") (<= 59 (Integer/parseInt n) 76)))
    false))

(defn valid? [r]
  (and (valid-keys? r)
       (<= 1920 (Integer/parseInt (r "byr")) 2002)
       (<= 2010 (Integer/parseInt (r "iyr")) 2020)
       (<= 2020 (Integer/parseInt (r "eyr")) 2030)
       (valid-height? (r "hgt"))
       (re-matches #"#[0-9a-f]{6}" (r "hcl"))
       (#{"amb" "blu" "brn" "gry" "grn" "hzl" "oth"} (r "ecl"))
       (re-matches #"[0-9]{9}" (r "pid"))))

(->> data
     (filter valid-keys?)
     (count)) ; => 228

(->> data
     (filter valid?)
     (count)) ; => 175