Last active
December 16, 2018 12:42
-
-
Save jaihindhreddy/f272a0055389e2b55129f2bc8cb8233b to your computer and use it in GitHub Desktop.
Advent of Code 2018 Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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