-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.adb
365 lines (323 loc) · 12.5 KB
/
day11.adb
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
with Ada.Text_IO;
procedure Day11 is
type Seat is
(Empty,
Occupied,
Floor,
Invalid);
type Char_To_Seat_T is Array (Character) of Seat;
Char_To_Seat : constant Char_To_Seat_T := Char_To_Seat_T'
('L' => Empty,
'#' => Occupied,
'.' => Floor,
others => Invalid);
type Seat_To_Char_T is Array (Seat) of Character;
Seat_To_Char : constant Seat_To_Char_T := Seat_To_Char_T'
(Empty => 'L',
Occupied => '#',
Floor => '.',
Invalid => 'X');
subtype Row_Count_T is Natural range 0 .. 100;
subtype Row_Num_T is Row_Count_T range 1 .. Row_Count_T'Last;
subtype Col_Count_T is Natural range 0 .. 100;
subtype Col_Num_T is Col_Count_T range 1 .. Col_Count_T'Last;
type Seating_X_T is Array (Col_Num_T) of Seat;
Empty_Row : constant Seating_X_T := (others => Invalid);
type Seating_X_Y_T is Array (Row_Num_T) of Seating_X_T;
Empty_Grid : constant Seating_X_Y_T := (others => Empty_Row);
type Seating_T is record
Seats : Seating_X_Y_T;
Col_Count : Col_Count_T;
Row_Count : Row_Count_T;
end record;
Empty_Seating : constant Seating_T := Seating_T'
(Seats => Empty_Grid,
Col_Count => 0,
Row_Count => 0);
Seating : Seating_T := Empty_Seating;
New_Seating : Seating_T := Empty_Seating;
Old_Seating : Seating_T := Empty_Seating;
Occupied_Seats : Natural := 0;
function Is_Empty_Or_Occupied
(Seating : Seating_T;
Row : Row_Count_T;
Col : Col_Count_T)
return Boolean is (Seating.Seats (Row) (Col) in Empty .. Occupied);
function Is_Occupied
(Seating : Seating_T;
Row : Row_Count_T;
Col : Col_Count_T)
return Boolean is (Seating.Seats (Row) (Col) = Occupied);
function Num_Occupied
(Seating : Seating_T;
Row : Row_Count_T;
Col : Col_Count_T)
return Natural
is
Adjacent_Count : Natural := 0;
begin
-- Test adjacent positions.
-- 1 2 3
-- 4 X 5
-- 6 7 8
if Row > Row_Num_T'First then
if Col > Col_Num_T'First then
if Is_Occupied (Seating, Row - 1, Col - 1) then -- Position 1
Adjacent_Count := Adjacent_Count + 1;
end if;
end if;
if Is_Occupied (Seating, Row - 1, Col) then -- Position 2
Adjacent_Count := Adjacent_Count + 1;
end if;
if Col < Seating.Col_Count then
if Is_Occupied (Seating, Row - 1, Col + 1) then -- Position 3
Adjacent_Count := Adjacent_Count + 1;
end if;
end if;
end if;
if Col > Col_Num_T'First then
if Is_Occupied (Seating, Row, Col - 1) then -- Position 4
Adjacent_Count := Adjacent_Count + 1;
end if;
end if;
if Col < Seating.Col_Count then
if Is_Occupied (Seating, Row, Col + 1) then -- Position 5
Adjacent_Count := Adjacent_Count + 1;
end if;
end if;
if Row < Seating.Row_Count then
if Col > Col_Num_T'First then
if Is_Occupied (Seating, Row + 1, Col - 1) then -- Position 6
Adjacent_Count := Adjacent_Count + 1;
end if;
end if;
if Is_Occupied (Seating, Row + 1, Col) then -- Position 7
Adjacent_Count := Adjacent_Count + 1;
end if;
if Col < Seating.Col_Count then
if Is_Occupied (Seating, Row + 1, Col + 1) then -- Position 8
Adjacent_Count := Adjacent_Count + 1;
end if;
end if;
end if;
return Adjacent_Count;
end Num_Occupied;
procedure Seat_Sim_1
(Seating : in Seating_T;
New_Seating : out Seating_T)
is
Adjacent_Count : Natural := 0;
begin
New_Seating.Row_Count := Seating.Row_Count;
New_Seating.Col_Count := Seating.Col_Count;
for Row in Row_Num_T'First .. Seating.Row_Count loop
for Col in Col_Num_T'First .. Seating.Col_Count loop
-- Ada.Text_IO.Put (Seat_To_Char(Seating.Seats (Row) (Col)));
Adjacent_Count := Num_Occupied (Seating, Row, Col);
case Seating.Seats (Row) (Col) is
when Floor | Invalid =>
New_Seating.Seats (Row) (Col) := Seating.Seats (Row) (Col);
when Empty =>
if Adjacent_Count = 0 then
New_Seating.Seats (Row) (Col) := Occupied;
else
New_Seating.Seats (Row) (Col) := Seating.Seats (Row) (Col);
end if;
when Occupied =>
if Adjacent_Count >= 4 then
New_Seating.Seats (Row) (Col) := Empty;
else
New_Seating.Seats (Row) (Col) := Seating.Seats (Row) (Col);
end if;
end case;
end loop;
-- ada.Text_IO.Put_Line ("");
end loop;
end Seat_Sim_1;
procedure Seat_Sim_2
(Seating : in Seating_T;
New_Seating : out Seating_T)
is
Adjacent_Count : Natural := 0;
Row_Limit : Natural := 0;
Col_Limit : Natural := 0;
The_Limit : Natural := 0;
begin
New_Seating.Row_Count := Seating.Row_Count;
New_Seating.Col_Count := Seating.Col_Count;
for Row in Row_Num_T'First .. Seating.Row_Count loop
for Col in Col_Num_T'First .. Seating.Col_Count loop
-- Ada.Text_IO.Put (Seat_To_Char(Seating.Seats (Row) (Col)));
Adjacent_Count := 0;
-- Test positions in a particular direction until we reach a seat.
-- 1 2 3
-- 4 X 5
-- 6 7 8
if Row > Row_Num_T'First then
if Col > Col_Num_T'First then
-- Find limits
Row_Limit := Row - Row_Num_T'First;
Col_Limit := Col - Col_Num_T'First;
if Row_Limit < Col_Limit then
The_Limit := Row_Limit;
else
The_Limit := Col_Limit;
end if;
for XY in 1 .. The_Limit loop
if Is_Occupied (Seating, Row - XY, Col - XY) then -- Position 1
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row - XY, Col - XY);
end loop;
end if;
for Y in 1 .. (Row - Row_Num_T'First) loop
if Is_Occupied (Seating, Row - Y, Col) then -- Position 2
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row - Y, Col);
end loop;
if Col < Seating.Col_Count then
Row_Limit := Row - Row_Num_T'First;
Col_Limit := Seating.Col_Count - Col;
if Row_Limit < Col_Limit then
The_Limit := Row_Limit;
else
The_Limit := Col_Limit;
end if;
for XY in 1 .. The_Limit loop
if Is_Occupied (Seating, Row - XY, Col + XY) then -- Position 3
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row - XY, Col + XY);
end loop;
end if;
end if;
if Col > Col_Num_T'First then
for Y in 1 .. Col - Col_Num_T'First loop
if Is_Occupied (Seating, Row, Col - Y) then -- Position 4
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row, Col - Y);
end loop;
end if;
if Col < Seating.Col_Count then
for Y in 1 .. Seating.Col_Count - Col loop
if Is_Occupied (Seating, Row, Col + Y) then -- Position 5
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row, Col + Y);
end loop;
end if;
if Row < Seating.Row_Count then
if Col > Col_Num_T'First then
Row_Limit := Seating.Row_Count - Row;
Col_Limit := Col - Col_Num_T'First;
if Row_Limit < Col_Limit then
The_Limit := Row_Limit;
else
The_Limit := Col_Limit;
end if;
for XY in 1 .. The_Limit loop
if Is_Occupied (Seating, Row + XY, Col - XY) then -- Position 6
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row + XY, Col - XY);
end loop;
end if;
for X in 1 .. Seating.Row_Count - Row loop
if Is_Occupied (Seating, Row + X, Col) then -- Position 7
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row + X, Col);
end loop;
if Col < Seating.Col_Count then
Row_Limit := Seating.Row_Count - Row;
Col_Limit := Seating.Col_Count - Col;
if Row_Limit < Col_Limit then
The_Limit := Row_Limit;
else
The_Limit := Col_Limit;
end if;
for XY in 1 .. The_Limit loop
if Is_Occupied (Seating, Row + XY, Col + XY) then -- Position 8
Adjacent_Count := Adjacent_Count + 1;
exit;
end if;
exit when Is_Empty_Or_Occupied (Seating, Row + XY, Col + XY);
end loop;
end if;
end if;
-- Ada.Text_IO.Put(Adjacent_Count'Img);
case Seating.Seats (Row) (Col) is
when Floor | Invalid =>
New_Seating.Seats (Row) (Col) := Seating.Seats (Row) (Col);
when Empty =>
if Adjacent_Count = 0 then
New_Seating.Seats (Row) (Col) := Occupied;
else
New_Seating.Seats (Row) (Col) := Seating.Seats (Row) (Col);
end if;
when Occupied =>
if Adjacent_Count >= 5 then
New_Seating.Seats (Row) (Col) := Empty;
else
New_Seating.Seats (Row) (Col) := Seating.Seats (Row) (Col);
end if;
end case;
end loop;
-- ada.Text_IO.Put_Line ("");
end loop;
end Seat_Sim_2;
begin
Seating.Row_Count := Seating.Row_Count + 1;
loop
declare
Line : constant String := Ada.Text_IO.Get_Line;
begin
Seating.Col_Count := 0;
for C in Line'Range loop
Seating.Col_Count := Seating.Col_Count + 1;
Seating.Seats (Seating.Row_Count) (Seating.Col_Count) := Char_To_Seat (Line (C));
end loop;
end;
exit when Ada.Text_IO.End_Of_File;
Seating.Row_Count := Seating.Row_Count + 1;
end loop;
Old_Seating := Seating;
Ada.Text_IO.Put_Line ("Read" & Seating.Row_Count'Img & Seating.Col_Count'Img);
loop
Seat_Sim_1 (Seating, New_Seating);
exit when Seating.Seats = New_Seating.Seats;
Seating := New_Seating;
end loop;
for Row in Row_Num_T'First .. Seating.Row_Count loop
for Col in Col_Num_T'First .. Seating.Col_Count loop
if Is_Occupied (Seating, Row, Col) then
Occupied_Seats := Occupied_Seats + 1;
end if;
end loop;
end loop;
Ada.Text_IO.Put_Line ("Occupied: " & Occupied_Seats'Img);
Seating := Old_Seating;
loop
Seat_Sim_2 (Seating, New_Seating);
exit when Seating.Seats = New_Seating.Seats;
Seating := New_Seating;
end loop;
Occupied_Seats := 0;
for Row in Row_Num_T'First .. Seating.Row_Count loop
for Col in Col_Num_T'First .. Seating.Col_Count loop
if Is_Occupied (Seating, Row, Col) then
Occupied_Seats := Occupied_Seats + 1;
end if;
end loop;
end loop;
Ada.Text_IO.Put_Line ("Occupied: " & Occupied_Seats'Img);
end Day11;