Skip to content

Instantly share code, notes, and snippets.

@jaihindhreddy
Last active December 16, 2018 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaihindhreddy/f272a0055389e2b55129f2bc8cb8233b to your computer and use it in GitHub Desktop.
Save jaihindhreddy/f272a0055389e2b55129f2bc8cb8233b to your computer and use it in GitHub Desktop.
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