diff options
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/03/solver.jl | 10 | ||||
-rw-r--r-- | AoC2022/aocclj/src/aocclj/day03.clj | 29 | ||||
-rw-r--r-- | AoC2022/aocclj/test/aocclj/core_test.clj | 19 | ||||
-rw-r--r-- | AoC2022/aocclj/test/aocclj/day01_test.clj | 10 |
4 files changed, 49 insertions, 19 deletions
diff --git a/AoC2022/03/solver.jl b/AoC2022/03/solver.jl index 13bb2a9..04d954b 100644 --- a/AoC2022/03/solver.jl +++ b/AoC2022/03/solver.jl @@ -5,11 +5,11 @@ data = open("input") do f end priority(x) = islowercase(x) ? x - 'a' + 1 : x - 'A' + 27 +s_halver(s) = s[1:length(s)÷2], s[length(s)÷2+1:end] + +solver(block) = (block .|> s-> intersect(s...)[1] .|> priority )|> sum @testset "solutions" begin - @test map( - s -> intersect(s[1:length(s)÷2], s[length(s)÷2+1:end])[1] |> priority, - data, - ) |> sum == 8072 - @test [intersect(data[i:i+2]...)[1] for i ∈ 1:3:length(data)] .|> priority |> sum == 2567 + @test data .|> s_halver |> solver == 8072 + @test [data[i:i+2] for i ∈ 1:3:length(data)] |> solver == 2567 end 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))) |