Day 3 - clojure

This commit is contained in:
Simponic 2022-12-03 17:39:59 -07:00
parent 846a6326f8
commit 5204d36f6b
Signed by untrusted user who does not match committer: simponic
GPG Key ID: 52B3774857EB24B1
2 changed files with 36 additions and 1 deletions

View File

@ -6,7 +6,8 @@
- [Day 0](./day-0) - [Day 0](./day-0)
- [x] C - [x] C
- [Day 1](./day-1) - [Day 1](./day-1)
- [] Clojure - [x] Clojure
- [Day 2](./day-2)
- [] Common LISP - [] Common LISP
- [] C++ - [] C++
- [] Dart - [] Dart

34
day-02/sol.clj Normal file
View 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)