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/84701ee7656a40a133a45315b9f6c8f9 to your computer and use it in GitHub Desktop.
Save jaihindhreddy/84701ee7656a40a133a45315b9f6c8f9 to your computer and use it in GitHub Desktop.
Advent of Code 2018 Day 3
(defn map-vals [f m]
(into {}
(map (fn [[k v]] [k (f v)]))
m))
(defn parse-line [s]
(->> (re-find #"^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)" s)
rest
(map #(Integer/parseInt %))
(map vector [:id :x :y :w :h])
(into {})))
(defn create-matrix
[{:keys [id x y w h] :as claim}]
(for [i (range x (+ x w))
j (range y (+ y h))]
[id i j]))
(defn overlaps? [pt->freq claim]
(->> (create-matrix claim)
(map rest)
(every? #(= 1 (pt->freq %)))
not))
(def input
(->> (slurp "input.txt")
clojure.string/split-lines
(map parse-line)))
(def pt->ids
(->> input
(mapcat create-matrix)
(group-by rest)
(map-vals #(set (map first %)))))
(def pt->freq
(map-vals count pt->ids))
(def part-1
(->> (vals pt->freq)
(filter #(>= % 2))
count))
(def part-2
(->> input
(filterv #(not (overlaps? pt->freq %)))
first
:id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment