Skip to content

Instantly share code, notes, and snippets.

Last active December 16, 2018 12:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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)]))
(defn parse-line [s]
(->> (re-find #"^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)" s)
(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 %)))
(def input
(->> (slurp "input.txt")
(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))
(def part-2
(->> input
(filterv #(not (overlaps? pt->freq %)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment