aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/aocclj
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/aocclj')
-rw-r--r--AoC2022/aocclj/src/aocclj/day03.clj29
-rw-r--r--AoC2022/aocclj/test/aocclj/core_test.clj19
-rw-r--r--AoC2022/aocclj/test/aocclj/day01_test.clj10
3 files changed, 44 insertions, 14 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))
diff --git a/AoC2022/aocclj/test/aocclj/core_test.clj b/AoC2022/aocclj/test/aocclj/core_test.clj
index 15216e7..dc3f3bb 100644
--- a/AoC2022/aocclj/test/aocclj/core_test.clj
+++ b/AoC2022/aocclj/test/aocclj/core_test.clj
@@ -1,7 +1,18 @@
(ns aocclj.core-test
(:require [clojure.test :refer :all]
- [aocclj.core :refer :all]))
+ [aocclj.day01 :as day01]
+ [aocclj.day03 :as day03]
+ [aocclj.core :refer :all]
+ [clojure.string :as str]))
-(deftest a-test
- (testing "FIXME, I fail."
- (is (= 1 1))))
+(deftest day01
+ (let [input (str/split-lines (slurp "../01/input"))]
+ (are [expected function] (= expected (function input))
+ 75622 day01/part1
+ 213159 day01/part2)))
+
+(deftest day03
+ (let [input (str/split-lines (slurp "../03/input"))]
+ (are [expected function] (= expected (function input))
+ 8072 day03/part1
+ 2567 day03/part2)))
diff --git a/AoC2022/aocclj/test/aocclj/day01_test.clj b/AoC2022/aocclj/test/aocclj/day01_test.clj
deleted file mode 100644
index ca1262c..0000000
--- a/AoC2022/aocclj/test/aocclj/day01_test.clj
+++ /dev/null
@@ -1,10 +0,0 @@
-(ns aocclj.day01-test
- (:require [aocclj.day01 :as sut]
- [clojure.test :as t]
- [clojure.string :as str]))
-
-(t/deftest solutions
- (let [input (str/split-lines (slurp "../01/input"))]
- (t/are [expected function] (= expected (function input))
- 75622 sut/part1
- 213159 sut/part2)))