From 99bcf55e2df61cef89cb88213e6feddd88294d47 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 6 Feb 2023 00:01:53 +0100 Subject: day 3 review --- AoC2022/aocclj/src/aocclj/day03.clj | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 AoC2022/aocclj/src/aocclj/day03.clj (limited to 'AoC2022/aocclj/src') 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)) -- cgit v1.2.3