diff options
Diffstat (limited to 'aoc_2022/day-02')
-rw-r--r-- | aoc_2022/day-02/.gitkeep | 0 | ||||
-rw-r--r-- | aoc_2022/day-02/sol.clj | 34 |
2 files changed, 34 insertions, 0 deletions
diff --git a/aoc_2022/day-02/.gitkeep b/aoc_2022/day-02/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/aoc_2022/day-02/.gitkeep diff --git a/aoc_2022/day-02/sol.clj b/aoc_2022/day-02/sol.clj new file mode 100644 index 0000000..99783cc --- /dev/null +++ b/aoc_2022/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 |