summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimponic <loganhunt@simponic.xyz>2022-12-03 17:39:59 -0700
committerSimponic <loganhunt@simponic.xyz>2022-12-03 17:40:35 -0700
commit5204d36f6b856abe15a013cfab190baaec1eba19 (patch)
tree9f29b1b60bb73f489ee15e5551c2814cbc518dc5
parent846a6326f87709d6b645e3fd20bde069c3fdf44b (diff)
downloadaoc-5204d36f6b856abe15a013cfab190baaec1eba19.tar.gz
aoc-5204d36f6b856abe15a013cfab190baaec1eba19.zip
Day 3 - clojure
-rw-r--r--README.md3
-rw-r--r--day-02/sol.clj34
2 files changed, 36 insertions, 1 deletions
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