/
day_06.clj
72 lines (59 loc) · 1.54 KB
/
day_06.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
65
66
67
68
69
70
71
72
(ns advent-of-code-2015.day-06
(:gen-class))
(defn turn-on
[_]
1)
(defn turn-off
[_]
0)
(defn toggle
[v]
(if (= v 1) 0 1))
(defn to-instruction
([s]
(condp #(clojure.string/includes? %2 %1) s
"turn on" (constantly 1)
"turn off" (constantly 0)
"toggle" #(condp = % 1 0 0 1)))
([_ s]
(condp #(clojure.string/includes? %2 %1) s
"turn on" inc
"turn off" #(max 0 (dec %))
"toggle" (partial + 2))))
(defn to-range
[s]
(let [[x1 y1 x2 y2 _] (map #(Integer/parseInt %) (re-seq #"[\d]+" s))]
(for [x (range x1 (inc x2))
y (range y1 (inc y2))]
(+ (* 1000 y) x))))
(defn update-grid
([grid [f range]]
(loop [t-grid (transient grid)
xy (first range)
xys (rest range)]
(if-not xy (persistent! t-grid)
(recur (assoc! t-grid xy (f (get t-grid xy)))
(first xys)
(rest xys))))))
(def off-grid (vec (repeat (* 1000 1000) 0)))
(defn part-1
[input]
(->> input
(clojure.string/split-lines)
(map #(vector (to-instruction %) (to-range %)))
(reduce update-grid off-grid)
(filter (partial = 1))
(count)))
(defn part-2
[input]
(->> input
(clojure.string/split-lines)
(map #(vector (to-instruction :proper %) (to-range %)))
(reduce update-grid off-grid)
(reduce +)))
(defn day-06
[input-file]
(let [input (slurp input-file)]
(println "Day 6")
(println "Number of lit lights: " (part-1 input))
(println "Total brightness: " (part-2 input))))