-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16b.c
340 lines (271 loc) · 7.23 KB
/
day16b.c
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
// Licensed under the MIT License.
// The Floor Will Be Lava Part 2
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define COORDINATE_STACK_CAPACITY 128
#define DIMENSION 128
#define EXCEPTION_FORMAT "Error: Format.\n"
enum Direction
{
DIRECTION_HI,
DIRECTION_LO,
DIRECTION_LEFT,
DIRECTION_RIGHT,
DIRECTION_NONE
};
struct Coordinate
{
int i;
int j;
enum Direction direction;
};
struct CoordinateStack
{
int count;
struct Coordinate items[COORDINATE_STACK_CAPACITY];
};
struct Table
{
int rows;
int columns;
char matrix[DIMENSION * DIMENSION];
bool set[DIMENSION * DIMENSION][DIRECTION_NONE];
};
typedef enum Direction Direction;
typedef enum Direction* DirectionDictionary;
typedef struct Coordinate* Coordinate;
typedef struct CoordinateStack* CoordinateStack;
typedef struct Table* Table;
void coordinate_stack(CoordinateStack instance)
{
instance->count = 0;
}
void coordinate_stack_push(CoordinateStack instance, Coordinate item)
{
instance->items[instance->count] = *item;
instance->count++;
}
bool coordinate_stack_try_pop(CoordinateStack instance, Coordinate result)
{
if (!instance->count)
{
return false;
}
instance->count--;
*result = instance->items[instance->count];
return true;
}
void table(Table instance, int columns)
{
instance->rows = 0;
instance->columns = columns;
}
char table_matrix_get(Table instance, Coordinate coordinate)
{
int index = (instance->columns * coordinate->i) + coordinate->j;
return instance->matrix[index];
}
void table_matrix_add_row(Table instance, char values[])
{
int m = instance->rows;
instance->rows = m + 1;
memcpy(
instance->matrix + (instance->columns * m),
values,
instance->columns);
}
bool table_set_add(Table instance, Coordinate coordinate)
{
int index = (coordinate->i * instance->columns) + coordinate->j;
if (instance->set[index][coordinate->direction])
{
return false;
}
instance->set[index][coordinate->direction] = true;
return true;
}
void table_set_clear(Table instance)
{
memset(
instance->set,
false,
instance->rows * instance->columns * DIRECTION_NONE);
}
static int scan(
Table table,
Coordinate initial,
DirectionDictionary hiRight,
DirectionDictionary loRight)
{
struct CoordinateStack stack;
struct Coordinate current = *initial;
table_set_clear(table);
table_set_add(table, ¤t);
coordinate_stack(&stack);
coordinate_stack_push(&stack, ¤t);
while (coordinate_stack_try_pop(&stack, ¤t))
{
switch (table_matrix_get(table, ¤t))
{
case '/':
current.direction = hiRight[current.direction];
break;
case '\\':
current.direction = loRight[current.direction];
break;
case '-':
{
if (current.direction == DIRECTION_LEFT ||
current.direction == DIRECTION_RIGHT)
{
break;
}
current.direction = DIRECTION_RIGHT;
struct Coordinate next =
{
.i = current.i,
.j = current.j,
.direction = DIRECTION_LEFT
};
coordinate_stack_push(&stack, &next);
}
break;
case '|':
{
if (current.direction == DIRECTION_HI ||
current.direction == DIRECTION_LO)
{
break;
}
current.direction = DIRECTION_HI;
struct Coordinate next =
{
.i = current.i,
.j = current.j,
.direction = DIRECTION_LO
};
coordinate_stack_push(&stack, &next);
}
break;
}
switch (current.direction)
{
case DIRECTION_HI:
current.i--;
break;
case DIRECTION_LO:
current.i++;
break;
case DIRECTION_LEFT:
current.j--;
break;
case DIRECTION_RIGHT:
current.j++;
break;
default: break;
}
if (current.i < 0 || current.i >= table->rows ||
current.j < 0 || current.j >= table->columns ||
!table_set_add(table, ¤t))
{
continue;
}
coordinate_stack_push(&stack, ¤t);
}
int result = 0;
for (int k = 0; k < table->rows * table->columns; k++)
{
for (Direction direction = 0; direction < DIRECTION_NONE; direction++)
{
if (table->set[k][direction])
{
result++;
break;
}
}
}
return result;
}
static void scan_vertical(
Table table,
Coordinate current,
DirectionDictionary hiRight,
DirectionDictionary loRight,
int* max)
{
for (current->i = 0; current->i < table->rows; current->i++)
{
int total = scan(table, current, hiRight, loRight);
if (total > *max)
{
*max = total;
}
}
}
static void scan_horizontal(
Table table,
Coordinate current,
DirectionDictionary hiRight,
DirectionDictionary loRight,
int* max)
{
for (current->j = 0; current->j < table->columns; current->j++)
{
int total = scan(table, current, hiRight, loRight);
if (total > *max)
{
*max = total;
}
}
}
int main(void)
{
char buffer[DIMENSION + 2];
clock_t start = clock();
if (!fgets(buffer, sizeof buffer, stdin))
{
fprintf(stderr, EXCEPTION_FORMAT);
return 1;
}
int n = strlen(buffer) - 1;
if (n < 1)
{
fprintf(stderr, EXCEPTION_FORMAT);
return 1;
}
struct Table data;
struct Coordinate current;
table(&data, n);
do
{
table_matrix_add_row(&data, buffer);
}
while (fgets(buffer, n + 2, stdin));
int max = 0;
Direction hiRight[DIRECTION_NONE];
Direction loRight[DIRECTION_NONE];
hiRight[DIRECTION_HI] = DIRECTION_RIGHT;
hiRight[DIRECTION_LO] = DIRECTION_LEFT;
hiRight[DIRECTION_LEFT] = DIRECTION_LO;
hiRight[DIRECTION_RIGHT] = DIRECTION_HI;
loRight[DIRECTION_HI] = DIRECTION_LEFT;
loRight[DIRECTION_LO] = DIRECTION_RIGHT;
loRight[DIRECTION_LEFT] = DIRECTION_HI;
loRight[DIRECTION_RIGHT] = DIRECTION_LO;
current.i = data.rows - 1;
current.direction = DIRECTION_HI;
scan_horizontal(&data, ¤t, hiRight, loRight, &max);
current.i = 0;
current.direction = DIRECTION_LO;
scan_horizontal(&data, ¤t, hiRight, loRight, &max);
current.j = data.columns - 1;
current.direction = DIRECTION_LEFT;
scan_vertical(&data, ¤t, hiRight, loRight, &max);
current.j = 0;
current.direction = DIRECTION_RIGHT;
scan_vertical(&data, ¤t, hiRight, loRight, &max);
printf("16b %d %lf\n", max, (double)(clock() - start) / CLOCKS_PER_SEC);
return 0;
}