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
|
#include <err.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define ROWCNT 98
#define ROWLEN 98
#ifdef PART2
#define IS_SEAT(a, b) \
if (rows[a][b] == '.') \
continue
#define IS_OCC(a, b) \
if (rows[a][b] == '#') \
adjecent++
#endif
bool changed;
char rows[ROWCNT][ROWLEN + 1];
char temp[ROWCNT][ROWLEN + 1];
/* Check and update a seat at the given index */
static void
check_seat(int const i, int const j)
{
if (rows[i][j] == '.')
return;
int adjecent = 0;
#ifdef PART2
/* Up */
for (int h = i - 1; h >= 0; h--) {
IS_SEAT(h, j);
IS_OCC(h, j);
break;
}
/* Down */
for (int h = i + 1; h < ROWCNT; h++) {
IS_SEAT(h, j);
IS_OCC(h, j);
break;
}
/* Left */
for (int h = j - 1; h >= 0; h--) {
IS_SEAT(i, h);
IS_OCC(i, h);
break;
}
/* Right */
for (int h = j + 1; h < ROWLEN; h++) {
IS_SEAT(i, h);
IS_OCC(i, h);
break;
}
/* Up-Left */
for (int h = i - 1, k = j - 1; h >= 0 && k >= 0; h--, k--) {
IS_SEAT(h, k);
IS_OCC(h, k);
break;
}
/* Up-Right */
for (int h = i - 1, k = j + 1; h >= 0 && k < ROWLEN; h--, k++) {
IS_SEAT(h, k);
IS_OCC(h, k);
break;
}
/* Down-Left */
for (int h = i + 1, k = j - 1; h < ROWCNT && k >= 0; h++, k--) {
IS_SEAT(h, k);
IS_OCC(h, k);
break;
}
/* Down-Right */
for (int h = i + 1, k = j + 1; h < ROWCNT && k < ROWLEN; h++, k++) {
IS_SEAT(h, k);
IS_OCC(h, k);
break;
}
#else
for (int h = i - 1; h <= i + 1; h++) {
if (h < 0 || h > ROWCNT - 1)
continue;
for (int k = j - 1; k <= j + 1; k++) {
if (k < 0 || k > ROWCNT - 1 || (h == i && k == j))
continue;
if (rows[h][k] == '#')
adjecent++;
}
}
#endif
/* Edit seats as needed */
if (rows[i][j] == 'L' && adjecent == 0) {
temp[i][j] = '#';
changed = true;
#ifdef PART2
} else if (rows[i][j] == '#' && adjecent >= 5) {
#else
} else if (rows[i][j] == '#' && adjecent >= 4) {
#endif
temp[i][j] = 'L';
changed = true;
} else
temp[i][j] = rows[i][j];
}
int
main(void)
{
/* Initialize the temp array to be all floor */
int i, j;
for (i = 0; i < ROWCNT; i++) {
for (j = 0; j < ROWLEN; j++)
temp[i][j] = '.';
}
FILE *fp;
if (!(fp = fopen("input", "r")))
err(EXIT_FAILURE, "fopen");
/* Read in seats */
i = j = 0;
char c;
while ((c = fgetc(fp)) != EOF) {
if (c == '\n') {
i = 0;
j++;
} else
rows[j][i++] = c;
}
fclose(fp);
/* Update the seats until they don't change anymore */
do {
changed = false;
for (i = 0; i < ROWCNT; i++) {
for (j = 0; j < ROWLEN; j++)
check_seat(i, j);
}
for (i = 0; i < ROWCNT; i++) {
for (j = 0; j < ROWLEN; j++)
rows[i][j] = temp[i][j];
}
} while (changed);
/* Calculate filled seats */
int count = 0;
for (i = 0; i < ROWCNT; i++) {
for (j = 0; j < ROWLEN; j++) {
if (rows[i][j] == '#')
count++;
}
}
printf("%d\n", count);
return EXIT_SUCCESS;
}
|