about summary refs log tree commit diff
path: root/day04.clj
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-12-05 17:44:23 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-12-05 17:44:23 +0100
commita2037e84ec9dd2ee9ad1528d94a5e4451a0fd41a (patch)
tree2d420888ff5b9aa35092007e0792df6549dc2df4 /day04.clj
parentd20125b66e35d3380e4e39c4a75fbb6604b6cc00 (diff)
downloadadventofcode2020-a2037e84ec9dd2ee9ad1528d94a5e4451a0fd41a.tar.gz
adventofcode2020-a2037e84ec9dd2ee9ad1528d94a5e4451a0fd41a.tar.xz
adventofcode2020-a2037e84ec9dd2ee9ad1528d94a5e4451a0fd41a.zip
day04
Diffstat (limited to 'day04.clj')
-rw-r--r--day04.clj34
1 files changed, 34 insertions, 0 deletions
diff --git a/day04.clj b/day04.clj
new file mode 100644
index 0000000..b33e838
--- /dev/null
+++ b/day04.clj
@@ -0,0 +1,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