aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/aocclj/src
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/aocclj/src')
-rw-r--r--AoC2022/aocclj/src/aocclj/day03.clj29
1 files changed, 29 insertions, 0 deletions
diff --git a/AoC2022/aocclj/src/aocclj/day03.clj b/AoC2022/aocclj/src/aocclj/day03.clj
new file mode 100644
index 0000000..dd3fb8d
--- /dev/null
+++ b/AoC2022/aocclj/src/aocclj/day03.clj
@@ -0,0 +1,29 @@
+(ns aocclj.day03
+ (:require [clojure.string :as str]
+ [clojure.set :as set]))
+
+(defn priority [c]
+ (->>
+ (if (Character/isLowerCase c)
+ (dec (int \a))
+ (- (int \A) 27))
+ (- (int c))))
+
+(defn seq-halver [s]
+ (split-at (/ (count s) 2) s))
+
+(defn block-priority [block]
+ ((comp priority first (partial apply set/intersection) (partial map set)) block))
+
+(defn solver [group-transducer input]
+ (transduce
+ (comp
+ group-transducer
+ (map block-priority))
+ + input))
+
+(defn part1 [input]
+ (solver (map seq-halver) input))
+
+(defn part2 [input]
+ (solver (partition-all 3) input))