Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Advent of Code 2018 Day 2
(def input
"set of strings representing box IDs"
(clojure.string/split-lines (slurp "input.txt")))
(defn twice-and-thrice [id]
(let [freqs (-> id frequencies vals set)]
[(if (freqs 2) 1 0)
(if (freqs 3) 1 0)]))
(defn elem-diff [a b]
"Returns the number of elements differing in a and b.
Lengths of a and b are assumed to be equal"
(->> (map vector a b)
(remove #(apply = %))
count))
(defn common-str [a b]
"Gets the common part of two strings a and b"
(->> (map vector a b)
(filter #(apply = %))
(map first)
(apply str)))
(def part-1
"Find the checksum of the seq of box IDs, all of the same length.
The checksum is the number of IDs which contain a letter that appears exactly twice
multiplied by the number of IDs which contain a letter that appears exactly thrice"
(->> input
(map twice-and-thrice)
(reduce #(map + %1 %2))
(apply *)))
(def part-2
"There is exactly one pair of IDs that differ only by one character.
Find the common part of this pair"
(first (for [a input
b input
:when (= 1 (elem-diff a b))]
(common-str a b))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment