What follows are little mini problems I solved in order to finished Day 2, 2018 of The Advent of Code.
Identify strings that have
ninstances of any character.
abcdefhas no repeated characters.
(defn has-n-repeats? [n input] (as-> (sort input) $ (partition-by char $) (filter #(= n (count %)) $) (not (empty? $))))
Count differences at corresponding positions between two strings.
fghijdiffer at all five locations.
fguijdiffer at one location.
Naive Solution: use set operations (union, intersection) to see how different the strings are.
xrlgqmavnskvzupalfiwhjctdbdiffer at five positions
In the advanced case, both strings have repeated characters, so sets aren't helpful.
Data Flow for
(f f g g h u i i j j)
((f f) (g g) (h u) (i i) (j j))
(->> (interleave a b) (partition 2) (remove #(= (first %) (second %))) (count))
(->> (interleave a b) (partition 2) (remove #(apply = %)) (count))
(def equal? (partial apply =)) (->> (interleave a b) (partition 2) (remove equal?) (count))
List characters that are equal at corresponding positions between two strings (preserving order of appearance).
(defn common-chars [a b] (->> (interleave a b) (partition 2) (filter equal?) (map first) (apply str)))
Among a list of strings, find the two that differ by one character at a single character position and return the common characters.
diff-countbetween every combination of strings
common-charsof that combination.
(defn common-between-diff-by-one [inputs] (first (for [x inputs y inputs :when (= (diff-count x y) 1)] (common-chars x y))))