-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.clj
32 lines (24 loc) · 853 Bytes
/
05.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
(ns alchemical-reduction)
(def polymer (-> "05.in" slurp clojure.string/trim-newline))
;; part 1
(defn eq-upper-or-lower [x y]
(and (not= x y) (apply = (map clojure.string/lower-case [x y]))))
(defn react [polymer]
(reduce (fn [coll x]
(if (and (seq coll) (eq-upper-or-lower x (first coll)))
(rest coll)
(cons x coll)))
[]
polymer))
(println (count (react polymer)))
;; part 2
(defn lowercase-eq [& args]
(apply = (map clojure.string/lower-case args)))
(defn remove-char [char string]
(remove #(lowercase-eq char %) string))
(def a-to-z (map char (range (int \a) (inc (int \z)))))
(println (apply min (map #(->> polymer
(remove-char %)
react
count)
a-to-z)))