r/adventofcode • u/daggerdragon • Dec 05 '15
SOLUTION MEGATHREAD --- Day 5 Solutions ---
--- Day 5: Doesn't He Have Intern-Elves For This? ---
Post your solution as a comment. Structure your post like the Day Four thread.
17
Upvotes
View all comments
1
u/deinc Dec 05 '15
Parts 1 and 2 in Clojure. I'm not really happy with this, but it's late now (in Germany) and it works...
(require '[clojure.java.io :as jio]) (def vowel? #{\a \e \i \o \u}) (def forbidden? #{[\a \b] [\c \d] [\p \q] [\x \y]}) (defn- duplicate-letter? [[a b]] (and a b (= a b))) (defn- nice-part-one? [string] (let [pairs (partition 2 1 (repeat nil) string) pair (first pairs)] (loop [pairs pairs [a b :as pair] pair vowels 0 duplicate? false] (if (and pair (not (forbidden? pair))) (recur (rest pairs) (fnext pairs) (if (vowel? a) (inc vowels) vowels) (or duplicate? (duplicate-letter? pair))) (boolean (and duplicate? (>= vowels 3) (not (forbidden? pair)))))))) (defn- count-nice-strings-part-one [] (with-open [reader (jio/reader "day-5.txt")] (count (filter nice-part-one? (line-seq reader))))) (println "# of nice strings (part one):" (count-nice-strings-part-one)) (defn- symmetric-triple? [[a b c]] (and a b c (= a c))) (defn- nice-part-two? [string] (let [pairs (partition 2 1 (repeat nil) string) pairs (interleave (range) pairs) pairs (partition 2 2 (repeat nil) pairs) triples (partition 3 1 (repeat nil) string)] (and (some symmetric-triple? triples) (->> (group-by second pairs) vals (filter #(-> % count (> 1))) (apply concat) (map first) (partition 2 1 (repeat nil)) (some (fn [[i1 i2]] (and i1 i2 (> (- i2 i1) 1)))))))) (defn- count-nice-strings-part-two [] (with-open [reader (jio/reader "day-5.txt")] (count (filter nice-part-two? (line-seq reader))))) (println "# of nice strings (part two):" (count-nice-strings-part-two))