-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc11.jl
323 lines (287 loc) · 14.2 KB
/
aoc11.jl
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# ---
# jupyter:
# jupytext:
# formats: ipynb,jl:percent
# text_representation:
# extension: .jl
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.7.1
# kernelspec:
# display_name: Julia 1.5.2
# language: julia
# name: julia-1.5
# ---
# %%
ex1 = "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"
ex1 = split(ex1, '\n');
# %%
input = "LLLLLLL.LLLLLLLLLLLLLLL.LLL.LLLL.LLLLLLL.L.LLLLLLL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LLLLLLL.LLLLLL.LLLLL.LLLLLLL.LLLLLLLLLL.L.LLLLLL.LLLLLLLL..LLLLL.LLLLLLLLL.LLLLLLLL
LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLL.LLL.LLLLLL.LL.LLLLLL.LLLLLLLL
LLLLLLLLLLLLLLLL.LLLLLL.LLL.L.LLLLLLL.LLLLLLLLLLLL.LLLLLLLLL.LLL.L.LLLLLL.LLLLLLLLLLLLLLLLLL
LLLLLLL.LLLLL.LL.LL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL..LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL
LLL.LLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLL"
input = split(input, '\n');
# %%
function to_array(los)
# los is list of strings
seats = Array{Char, 2}(undef, length(los), length(los[1]))
rows = length(los)
cols = length(los[1])
for row in 1:rows
for col in 1:cols
seats[row, col] = los[row][col]
end
end
return seats
end
# %%
ex1 = to_array(ex1)
input = to_array(input);# doing this because strings are inmutable
# %%
function grid_print(array)
string = ""
for r in 1:size(array)[1]
for c in 1:size(array)[2]
string = string*array[r, c]
end
string = string*'\n'
end
print(string)
end
# %%
function find_a_seat(input, verbose = false)
# one cycle
output = copy(input)
num_changes = 0
rows = size(input)[1]
cols = size(input)[2]
for row in 1:rows
for col in 1:cols
neighbors = 0
for i in -1:1, j in -1:1
if i == j == 0
continue # skip iter
elseif row == 1 && (i == -1)
continue
elseif col == 1 && j == -1
continue
elseif col == cols && j == 1
continue
elseif row == rows && i ==1
continue
end
if input[row+i, col+j] == '#'
neighbors +=1
end
end
if neighbors > 3 && input[row, col] == '#'
output[row, col] = 'L'
num_changes +=1
elseif neighbors == 0 && input[row, col] == 'L'
output[row, col] = '#'
num_changes +=1
end
end
end
if verbose
grid_print(input)
print("\n\n")
grid_print(output)
print("\n\n---------------\n\n")
end
if num_changes == 0
print("No more changes\n")
if verbose grid_print(output) end
end
return output, num_changes
end
# %%
function converge(fun, input; verbose = false)
output = copy(input)
num_changes = 1
while num_changes !=0
output, num_changes = fun(output, verbose)
end
if verbose grid_print(output) end
length(findall(==('#'), output))
end
# %%
converge(find_a_seat, ex1)
# %%
converge(find_a_seat, input)
# %% [markdown]
# ## Part 2
# %%
function find_a_seat2(input, verbose = false)
# one cycle
output = copy(input)
num_changes = 0
rows = size(input)[1]
cols = size(input)[2]
debug = Array{Int, 2}(undef, rows, cols)
for row in 1:rows
for col in 1:cols
neighbors = 0
for i in -1:1, j in -1:1
if i == j == 0
continue # skip iter
elseif row == 1 && i == -1
continue
elseif row == rows && i ==1
continue
elseif col == 1 && j == -1
continue
elseif col == cols && j == 1
continue
end
border_or_seat = false
step = 1
while !border_or_seat
if input[row+i*step, col+j*step] == '#'
neighbors +=1
border_or_seat = true # occupied seat found
elseif input[row+i*step, col+j*step] == 'L'
border_or_seat = true # empty seat found
end
if ((row+i*step == 1) && i!=0) ||
((col+j*step == 1) && j!=0) ||
((row+i*step == rows) && i!=0) ||
((col+j*step == cols) && j!=0)
border_or_seat = true # border found
end
step +=1
end
end
debug[row, col] = neighbors
if neighbors > 4 && input[row, col] == '#'
output[row, col] = 'L'
num_changes +=1
elseif neighbors == 0 && input[row, col] == 'L'
output[row, col] = '#'
num_changes +=1
end
end
end
if verbose
display(input)
print("\n\n")
display(debug)
print("\n\n")
display(output)
print("\n\n---------------\n\n")
end
if num_changes == 0
print("No more changes\n")
if verbose grid_print(output) end
end
return output, num_changes
end
# %%
converge(find_a_seat2, ex1)
# %%
converge(find_a_seat2, input)
# %%