-
Notifications
You must be signed in to change notification settings - Fork 2
/
3.clj
64 lines (54 loc) · 1.81 KB
/
3.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(ns advent-of-code.2018.3
(:require [advent-of-code.elves :refer :all]))
(defn claim->coords [{:keys [x-offset y-offset width height]}]
(for [x (range x-offset (+ x-offset width))
y (range y-offset (+ y-offset height))]
[x y]))
(def claims
(->> (day->input-lines 2018 3)
(map #(->> (re-matches #"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)" %)
(rest)
(map parse-long)))
(map #(zipmap [:claim-id :x-offset :y-offset :width :height] %))
(map #(assoc % :coords (claim->coords %)))))
(defn update-keys [m ks f & args]
(reduce (fn [m k] (apply update m k f args)) m ks))
;; solve part one
(def cloth
(reduce
(fn [acc {:keys [coords claim-id]}] (update-keys acc coords conj claim-id))
{}
claims))
(count (filter (fn [[_k v]] (< 1 (count v))) cloth))
;; solve part two
(->> claims
(filter #(every? (fn [c] (= 1 (count (cloth c))))
(:coords %)))
(first)
(:claim-id))
(comment
"Render animated elven tapestry."
(require '[quil.core :as q]
'[quil.middleware :as m])
(defn draw-state [{:keys [claims]}]
(if-let [claim (first claims)]
(let [{:keys [x-offset y-offset width height color]} claim]
(apply q/fill (conj color 192))
(q/rect x-offset y-offset width height))
(q/no-loop)))
(def colors
(let [step (range 0 256 25)]
(for [r step g step b step] [r g b])))
(defn setup []
(q/frame-rate 60)
(q/background 255)
{:claims (map #(assoc %1 :color %2) claims colors)})
(q/defsketch matrix
:title "No Matter How You Slice It"
:size [(apply max (map first (keys cloth)))
(apply max (map second (keys cloth)))]
:setup setup
:update #(update % :claims rest)
:draw draw-state
:features [:keep-on-top]
:middleware [m/fun-mode]))