Last active
December 16, 2018 12:42
-
-
Save jaihindhreddy/84701ee7656a40a133a45315b9f6c8f9 to your computer and use it in GitHub Desktop.
Advent of Code 2018 Day 3
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
(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