Day 3 - clojure
This commit is contained in:
parent
846a6326f8
commit
5204d36f6b
@ -6,7 +6,8 @@
|
||||
- [Day 0](./day-0)
|
||||
- [x] C
|
||||
- [Day 1](./day-1)
|
||||
- [] Clojure
|
||||
- [x] Clojure
|
||||
- [Day 2](./day-2)
|
||||
- [] Common LISP
|
||||
- [] C++
|
||||
- [] Dart
|
||||
|
34
day-02/sol.clj
Normal file
34
day-02/sol.clj
Normal file
@ -0,0 +1,34 @@
|
||||
(require '[clojure.java.io])
|
||||
|
||||
(defn find-recurring-characters [strs]
|
||||
(reduce (fn [a x]
|
||||
(into #{} (filter #(contains? a %) x)))
|
||||
(into #{} (first strs))
|
||||
strs))
|
||||
|
||||
(defn get-priority [c]
|
||||
(if (>= c 97)
|
||||
(- c 96)
|
||||
(- c 38)))
|
||||
|
||||
(defn obtain-total-priorities [rucksacks]
|
||||
(reduce + (map (fn [line]
|
||||
(let [half (/ (count line) 2)]
|
||||
(get-priority (int
|
||||
(first (find-recurring-characters
|
||||
(list (subs line 0 half)
|
||||
(subs line half))))))))
|
||||
rucksacks)))
|
||||
|
||||
(defn obtain-total-priorities-2 [rucksacks]
|
||||
(reduce + (map (fn [lines]
|
||||
(get-priority (int (first (find-recurring-characters lines)))))
|
||||
(partition 3 rucksacks))))
|
||||
|
||||
(defn main []
|
||||
(with-open [rdr (clojure.java.io/reader "input")]
|
||||
(println (obtain-total-priorities (line-seq rdr))))
|
||||
(with-open [rdr (clojure.java.io/reader "input")]
|
||||
(println (obtain-total-priorities-2 (line-seq rdr))))
|
||||
(System/exit 0))
|
||||
(main)
|
Loading…
Reference in New Issue
Block a user