-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.clj
142 lines (110 loc) · 3.34 KB
/
day04.clj
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(ns advent.day04
(:require [advent.core :as c]
[clojure.java.io :as io]
[clojure.pprint :as pp]
[clojure.string :as str]))
(defn build-maps [s]
(loop [wrds (str/split (str/replace s #"\n" " ") #" ")
acc {}]
(if (empty? wrds)
acc
(let [[k v] (str/split (first wrds) #":")]
(recur (rest wrds) (assoc acc (keyword k) v))))))
(defn read-input [file]
(-> file
(io/resource)
(slurp)
(str/split #"\n\n")
((partial map build-maps))
))
;; Valid password has all eight fields
;; Valid if only if missing the cid field
;; byr (Birth Year)
;; iyr (Issue Year)
;; eyr (Expiration Year)
;; hgt (Height)
;; hcl (Hair Color)
;; ecl (Eye Color)
;; pid (Passport ID)
;; cid (Country ID)
(def fields (set [:byr
:iyr
:eyr
:hgt
:hcl
:ecl
:pid
:cid]))
(defn valid [m]
(let [m-fields (set (keys m))]
(or (= fields m-fields)
(= (disj fields :cid) m-fields ))))
(defn part1 [file]
(->> file
read-input
(filter #(valid %))
count))
;; ----------------------------------------------------------------------------------------------------
;; Part2
(defn all-digits [cnt s]
(let [lst (seq s)]
(if (= cnt (count lst))
(every? true? (map #(Character/isDigit %) lst)))))
(defn valid-year [s n l h]
(and (all-digits n s)
(let [n (Integer/parseInt s)]
(and (>= n l)
(<= n h)))))
(defn valid-birth-year [s]
;; Four digits at least 1920 and at most 2002
(valid-year s 4 1920 2002))
(defn valid-issue-year [s]
;; Four digits at least 2010 and at most 2020
(valid-year s 4 2010 2020))
(defn valid-expiration-year [s]
;; four digits at least 2020 and at most 2030
(valid-year s 4 2002 2030))
(defn valid-height [s]
;; A number followed by either cm or in:
;; If cm, the number must be at least 150 and at most 193
;; If in, the number must be at least 59 and at most 76
;; Can have a missing cm/in which makes the value invalid as well
(let [len (count s)
[n t] (map #(apply str %) (split-at (- len 2) (seq s)))]
(if (or (= t "cm") (= t "in"))
(let [num (Integer/parseInt n)]
(if (= t "cm")
(and (>= num 150)
(<= num 193))
(if (= t "in")
(and (>= num 59)
(<= num 76))))))))
(defn valid-hair-color [s]
;; A # followed by exactly six characters 0-9 or a-f
(and (= \# (get s 0))
(= 7 (count s))
(= 6 (count (re-seq #"[a-z0-9]" (apply str (rest s)))))))
(defn valid-eye-color [s]
;; Exactly one of: amb blu brn gry grn hzl oth
(let [k (keyword s)
colors #{:amb :blu :brn :gry :grn :hzl :oth}]
(colors k)))
(defn num-digits [s]
(count (map true? (map #(Character/isDigit %) (seq s)))))
(defn valid-password [s]
;; A nine-digit number, including leading zeroes
(= 9 (num-digits s)))
(defn extended-valid [m]
(and (valid m)
(valid-birth-year (:byr m))
(valid-issue-year (:iyr m))
(valid-expiration-year (:eyr m))
(valid-height (:hgt m))
(valid-hair-color (:hcl m))
(valid-eye-color (:ecl m))
(valid-password (:pid m))))
(defn part2 [file]
(->> file
read-input
(filter #(extended-valid %))
count))