aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-02-06 00:01:53 +0100
committerOscar Najera <hi@oscarnajera.com>2023-02-06 00:01:53 +0100
commit99bcf55e2df61cef89cb88213e6feddd88294d47 (patch)
tree4aba0a910a4b0f1f8819346f7fc9b9da06dc5999 /AoC2022
parent57e563bf8f4fae7364118619318b338f16792473 (diff)
downloadscratch-99bcf55e2df61cef89cb88213e6feddd88294d47.tar.gz
scratch-99bcf55e2df61cef89cb88213e6feddd88294d47.tar.bz2
scratch-99bcf55e2df61cef89cb88213e6feddd88294d47.zip
day 3 review
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/03/solver.jl10
-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
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)))