-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_18.clj
86 lines (76 loc) · 1.91 KB
/
day_18.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(ns advent-of-code-2015.day-18
(:gen-class))
(defn parse-input
[input]
(->> input
(clojure.string/split-lines)
(mapv (partial mapv (partial = \#)))
))
(defn tick-bulb
[grid j i me]
(let [lit-neighbours (count
(filter identity
(map #(get-in grid % false)
[[(inc j) (dec i)] [(inc j) i] [(inc j) (inc i)]
[j (dec i)] [j (inc i)]
[(dec j) (dec i)] [(dec j) i] [(dec j) (inc i)]])))]
(if me (<= 2 lit-neighbours 3) (= 3 lit-neighbours))))
(defn tick-grid
[grid]
(->> grid
(map-indexed #(map-indexed (partial tick-bulb grid %1) %2))
(mapv (partial mapv identity))))
(def test-grid
(parse-input
".#.#.#
...##.
#....#
..#...
#.#..#
####.."))
(defn show-grid
[grid]
(->> grid
(map (partial map #(if % \# \.)))
(map (partial apply str))
(clojure.string/join \newline)
(println)
))
(defn part-1
[input]
(->> input
(parse-input)
(iterate tick-grid)
(drop 100)
(first)
(flatten)
(filter identity)
(count)))
(defn tick-broken-grid
[grid]
(-> grid
(assoc-in [0 0] true)
(assoc-in [0 99] true)
(assoc-in [99 0] true)
(assoc-in [99 99] true)
(tick-grid)
(assoc-in [0 0] true)
(assoc-in [0 99] true)
(assoc-in [99 0] true)
(assoc-in [99 99] true)))
(defn part-2
[input]
(->> input
(parse-input)
(iterate tick-broken-grid)
(drop 100)
(first)
(flatten)
(filter identity)
(count)))
(defn day-18
[input-file]
(let [input (slurp input-file)]
(println "Day 18")
(println "Number of lights on after 100 iterations: " (part-1 input))
(println "Number of lights on after 100 broken iterations: " (part-2 input))))