-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.c
267 lines (228 loc) · 7.08 KB
/
day11.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
// Advent of Code - Day 11
// @curiouskiwi
// 12 Dec 2020
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define taken(a) (a == '#')
#define empty(a) (a == 'L')
int partone(int h, int w, char temp[h][w], char spots[h][w]);
int parttwo(int h, int w, char temp[h][w], char spots[h][w]);
int occupyct(int h, int w, char spots[h][w]);
void copyseats(int h, int w, char dest[h][w], char src[h][w]);
void printseats(int h, int w, char s[h][w]);
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("usage: ./solve [part]\n");
return -1;
}
int choice = atoi(argv[1]);
if (choice < 1 || choice > 2)
{
printf("choice is 1 or 2\n");
return -1;
}
int width = 94; //94; // 10
int height = 93; //93;// 10
FILE *f = fopen("data.txt", "r");
if (!f)
{
printf("couldn't open data file\n");
return -1;
}
// has +2 to hold the newline and nul char but we'll ignore those going forward
char (*spots)[width] = calloc(height, width+2);
for (int i = 0; i < height; i++)
fgets(spots[i], width+2, f);
fclose(f);
// printseats(height, width, spots);
// temp array
char temp[height][width];
copyseats(height, width, temp, spots);
int switches = 0;
do
{
if (choice == 1)
switches = partone(height, width, temp, spots);
else
switches = parttwo(height, width, temp, spots);
}
while (switches);
printf("Part %i: %i\n", choice, occupyct(height, width, spots));
free(spots);
}
int partone (int h, int w, char temp[h][w], char spots[h][w])
{
// If a seat is empty (L) and there are no occupied seats adjacent to it,
// the seat becomes occupied.
// If a seat is occupied (#) and four or more seats adjacent to it are also
// occupied, the seat becomes empty.
// Otherwise, the seat's state does not change.
int switches = 0;
// variables to hold the up/down/around values
int xmin, xmax, ymin, ymax;
// iterate over rows
for (int i = 0; i < h; i++)
{
// if we are on the top or bottom row
ymin = (i == 0) ? 0 : -1;
ymax = (i == h - 1) ? 0 : 1;
// iterate over columns
for (int j = 0; j < w; j++)
{
// if we are on the left or right side column
xmin = (j == 0) ? 0 : -1;
xmax = (j == w - 1) ? 0 : 1;
//printf("%i:%i:%c\n", i, j, spots[i][j]);
//iterate over surrounding seats
char seat = spots[i][j];
if (seat != '.')
{
int countL = 0, countS = 0, count = 0;
for (int y = ymin; y <= ymax; y++)
{
for (int x = xmin; x <= xmax; x++)
{
char chk = spots[i+y][j+x];
// don't want to check the actual seat
if (!(y == 0 && x == 0))
{
if (chk != '.')
count++;
// we want to check this one
if (empty(chk))
countL++;
else if(taken(chk))
countS++;
}
}
}
// if seat is empty and no occupied adjacent, change to #
if (empty(seat) && countL == count)
{
temp[i][j] = '#';
switches++;
}
// if seat is occupied and 4+ seats are also occupied, set to L
if (taken(seat) && countS >= 4)
{
temp[i][j] = 'L';
switches++;
}
}
}
}
// printf("switches: %i\n", switches);
//switches = 0;
copyseats(h, w, spots, temp);
//printseats(height, width, spots);
//printf("occupied: %i\n", occupied);
return switches;
}
int parttwo(int h, int w, char temp[h][w], char spots[h][w])
{
// the eight directions we'll be searching
struct
{
int dx;
int dy;
int xLim;
int yLim;
}
dir[8] = {
{-1, -1, -1, -1}, // left/up
{+1, -1, w, -1}, // right/up
{+1, +1, w, h}, // right/down
{-1, +1, -1, h}, // left/down
{-1, 0, -1, -1}, // left
{+1, 0, w, -1}, // right
{0, -1, -1, -1}, // up
{0, +1, -1, h} // down
};
int switches = 0;
int countL, countS, checked;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
// this is the seat to check from
char seat = spots[i][j];
countL = countS = checked = 0;
if (seat != '.')
{
int x, y;
// look in all 8 directions
for (int d = 0; d < 8; d++)
{
// skip over the spots that are not seats
for (y = i + dir[d].dy, x = j + dir[d].dx;
y != dir[d].yLim && x != dir[d].xLim && spots[y][x] == '.';
y += dir[d].dy, x += dir[d].dx);
// we've found a seat
if (y != dir[d].yLim && x != dir[d].xLim)
{
checked++;
if (empty(spots[y][x]))
countL++;
else if (taken(spots[y][x]))
countS++;
}
}
// printf("%c:%i%i: L: %i S: %i chk: %i\n", seat, i, j, countL, countS, checked);
// if seat is empty and no occupied seen, change to #
if (empty(seat) && countL == checked)
{
temp[i][j] = '#';
switches++;
}
// if seat is occupied and 5+ seats are also occupied, set to L
if (taken(seat) && countS >= 5)
{
temp[i][j] = 'L';
switches++;
}
}
}
}
// set the spots to the new values for next round
copyseats(h, w, spots, temp);
return switches;
}
int occupyct(int h, int w, char spots[h][w])
{
int occupied = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
if (taken(spots[i][j]))
occupied++;
}
}
return occupied;
}
void copyseats(int h, int w, char dest[h][w], char src[h][w])
{
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
dest[i][j] = src[i][j];
}
}
}
void printseats(int h, int w, char s[h][w])
{
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
printf("%c", s[i][j]);
}
printf("\n");
}
printf("________________\n");
}