-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.cljs
167 lines (153 loc) · 12 KB
/
11.cljs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(require '[clojure.string :as str])
(def test-seats ["L.LL.LL.LL"
"LLLLLLL.LL"
"L.L.L..L.."
"LLLL.LL.LL"
"L.LL.LL.LL"
"L.LLLLL.LL"
"..L.L....."
"LLLLLLLLLL"
"L.LLLLLL.L"
"L.LLLLL.LL"])
(def seats ["LLLLLLLLLLLL.LLLLLLLLLL.LLLL.LLLL.LLLLLLLLLLL.LLLL.LLLLLLL.LL.LLLLLLLL.LLL.LLLLLLLLLLL.LLLLLLL"
"LLLLLLLLLLL.LLLL.L..LLL.LL.LLLLLLLLLLL.LLLLLL.LLLLLLLL.L.LLLLL.LLLLLL..LLL.LLLLLLLLLLLLLLLLLLL"
"LLLLLL.LLLL.LLLL.L.LLLL.LLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL..LLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL"
"LLLLLLLLLLL.LL.LL..LLLL.LLLL.L.LLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLL"
"LLLL.L.L.L..LLLLLLLLLL..LLL..LLLL.LLLL.LLLLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLLLLLLLLL"
"LL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LLLLLLLLLLLLLLLLLLLL"
"LLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL."
"L....L.....L..L......L....L.L...L.L.....L.L...LL......L....LL...L....LL...LL.......LLL...L.L.L"
"LLLLLLLLLLL.LLLLLLLL.LL.LLL..LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLL.LLLL.LLLLLLLLLLLLLL"
"LLLLLLLLLLLLLLLLLLLLLLLLLLL..LLLLLLLLL.LLLLLL..LLLLLLL.LLLLLL.L.L.LLLLLLLLL.LLL.LLLLLLLLLLLLLL"
"LLLLLL.LLLL.LLLLLL.LLLL.LL.LLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLL...LLLLLLLLLLLLLLLL.LLLLLLLLLLLLL."
"LLLLLL.LLLL.LLLLLL.LL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLL.L.LLLLLLLLLLLLLL"
"LLLLLL.LLLL.LLLLLLLLLLL.LLLL.LLLLL.LLLLLLLLLL.LLLLLLLL..LLLLL.LL.LLLLL.LLL.LL.L.LLLLLLLLLLLLLL"])
(def OCCUPIED "#")
(def EMPTY "L")
(def FLOOR ".")
(defn get-seat
[x y seats]
(nth (nth seats y "") x nil))
(defn next-seat
[[x y] seats skip [dx dy]]
(if (not skip)
(get-seat (+ x dx) (+ y dy) seats)
(loop [nx (+ x dx)
ny (+ y dy)]
(if (not= (get-seat nx ny seats) FLOOR)
(get-seat nx ny seats)
(recur (+ nx dx) (+ ny dy))))))
(defn neighbours
[x y seats skip]
(map #(next-seat [x y] seats skip %) [[-1 -1] [0 -1] [1 -1] [-1 0] [1 0] [-1 1] [0 1] [1 1]]))
(defn neighbour-count
[x y seats skip]
(count (filter (partial = OCCUPIED) (neighbours x y seats skip))))
(defn step
[rules x y seats]
(let [seat (get-seat x y seats)
num-neighbours (neighbour-count x y seats (rules :skip-floor))]
(cond
(= seat FLOOR) FLOOR
(= seat EMPTY) (if (= num-neighbours 0) OCCUPIED EMPTY)
(= seat OCCUPIED) (if (>= num-neighbours (rules :leave-at)) EMPTY OCCUPIED))))
(defn next-iteration
[seats rules]
(map-indexed
(fn [y-idx y]
(reduce str "" (map-indexed
(fn [x-idx _] (rules x-idx y-idx seats))
y)))
seats))
(defn stable-seats [seats rules] (loop [prev seats
next (next-iteration seats rules)
count 0]
(if (= prev next)
next
(do
(println count)
(recur next (next-iteration next rules) (inc count))))))
(defn count-occupied [seats] (->> seats
str/join
(filter (partial = OCCUPIED))
count))
(println (count-occupied (stable-seats seats (partial step {:skip-floor false :leave-at 4})))) ;; 2178
(println (count-occupied (stable-seats seats (partial step {:skip-floor true :leave-at 5})))) ;; 1978