From 5204d36f6b856abe15a013cfab190baaec1eba19 Mon Sep 17 00:00:00 2001 From: Simponic Date: Sat, 3 Dec 2022 17:39:59 -0700 Subject: [PATCH] Day 3 - clojure --- README.md | 3 ++- day-02/sol.clj | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 day-02/sol.clj diff --git a/README.md b/README.md index 2932562..75a170d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/day-02/sol.clj b/day-02/sol.clj new file mode 100644 index 0000000..99783cc --- /dev/null +++ b/day-02/sol.clj @@ -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) \ No newline at end of file