-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.c
319 lines (278 loc) · 6.87 KB
/
4.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINES 1030
#define MAX_PASSPORT_LENGTH 1000
#define FIELDS_COUNT 8
#define MAX_LINE_LENGTH 100
int getValidCount(FILE *in);
char *getNextPassport(FILE *in);
// Returns NULL if the passport isn't valid
char **getPassportFields(FILE *in);
int isDelimiter(char c);
int isEndOfLine(char c);
int getSuperValidCount(FILE *in);
int getValidParamCount(char *passport);
int validateBirthyear(char *content);
int validateIssueYear(char *content);
int validateExpirationYear(char *content);
int validateHeight(char *content);
int validateHairColor(char *content);
int validateEyeColor(char *content);
int validatePassportId(char *content);
int validateCountry(char *content);
int isNumber(char *content);
int isNumberN(char *content, int n);
int isDigit(char c);
int isSuperValid(char *field);
int main (int argc, char *argv[])
{
FILE *in = fopen("in4", "r");
int validCount = getValidCount(in);
rewind(in);
int superValidCount = getSuperValidCount(in);
printf("Valid count is: %d\nSuper valid count is: %d\n", validCount, superValidCount);
fclose(in);
return 0;
}
int getSuperValidCount(FILE *in)
{
char **fields;
int count = 0;
loop:
while ((fields = getPassportFields(in)) != NULL)
{
if (*fields == NULL)
// Not enough fields
continue;
for (int i = 0; i < FIELDS_COUNT; i++)
if (!isSuperValid(fields[i]))
{
// Passport is invalid - we don't need to check the rest
free(fields);
goto loop;
}
count++;
free(fields);
}
return count;
}
int isSuperValid(char *field)
{
if (field == NULL)
// cid must be missing - we already validated that all other fields must be present
return 1;
int validated = 0;
char *sub = calloc(3, sizeof(*sub));;
memcpy(sub, field, 3);
if (strcmp(sub, "byr") == 0)
return validateBirthyear(field+4);
if (strcmp(sub, "iyr") == 0)
return validateIssueYear(field+4);
if (strcmp(sub, "eyr") == 0)
return validateExpirationYear(field+4);
if (strcmp(sub, "ecl") == 0)
return validateEyeColor(field+4);
if (strcmp(sub, "hgt") == 0)
return validateHeight(field+4);
if (strcmp(sub, "hcl") == 0)
return validateHairColor(field+4);
if (strcmp(sub, "pid") == 0)
return validatePassportId(field+4);
if (strcmp(sub, "cid") == 0)
return validateCountry(field+4);
fprintf(stderr, "Unexpected: %s\n", sub);
return 0;
free(field);
}
char **getPassportFields(FILE *in)
{
char *passport = getNextPassport(in);
// Needed for free
char *originalPassportPointer = passport;
if (passport == NULL)
// Last passport has already passed
return NULL;
char **fields = calloc(FIELDS_COUNT, sizeof(*fields));
// We don't need _all_ fields - one may be missing
if (getValidParamCount(passport) != FIELDS_COUNT - 1)
{
free(passport);
// This will return a pointer to a NULL pointer
return fields;
}
char *currentLine = calloc(MAX_LINE_LENGTH, sizeof(*currentLine));
int j = 0, i = 0;
for (; passport[i] != '\0'; i++)
{
if (!isDelimiter(passport[i]))
{
currentLine[i] = passport[i];
continue;
}
if (i > 0)
{
fields[j] = currentLine;
currentLine = calloc(MAX_LINE_LENGTH, sizeof(*currentLine));
j++;
}
passport += i + 1;
i = -1;
}
if (i > 0)
{
printf("Got one last line\n");
fields[j] = currentLine;
}
else
free(currentLine);
free(originalPassportPointer);
return fields;
}
char *getNextPassport(FILE *in)
{
char *passport = malloc(MAX_PASSPORT_LENGTH * sizeof(*passport));
char *currentLine = NULL;
size_t read = 0;
size_t lineLength = 0;
while (getline(¤tLine, &lineLength, in) > 0)
{
if (isEndOfLine(*currentLine))
// We're done
break;
sprintf(passport + read, "%s", currentLine);
read += strlen(currentLine);
lineLength = 0;
currentLine = NULL;
}
if (read == 0)
{
// Got nothing
free(passport);
return NULL;
}
return passport;
}
int isDelimiter(char c)
{
return isEndOfLine(c) || c == ' ';
}
int isEndOfLine(char c)
{
return c == '\0' || c == '\n' || c == '\r';
}
int getValidCount(FILE *in)
{
char *currentLine;
size_t lineLength = 0;
int currentParamCount = 0;
int validCount = 0;
while (getline(¤tLine, &lineLength, in) > 0)
{
if (*currentLine == '\0' || *currentLine == '\n' || *currentLine == '\r')
{
// Blank line -> new passport
if (currentParamCount == 7)
validCount++;
currentParamCount = 0;
continue;
}
currentParamCount += getValidParamCount(currentLine);
}
if (currentParamCount == 7)
validCount++;
return validCount;
}
int getValidParamCount(char *passport)
{
char *required[] = {"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"/*, "cid"*/};
int currentParamCount = 0;
for (int i = 0; i < sizeof(required) / sizeof(*required); i++)
if (strstr(passport, required[i]) != NULL)
currentParamCount++;
return currentParamCount;
}
int validateBirthyear(char *content)
{
int year;
if (!isNumber(content))
return 0;
if (sscanf(content, "%d", &year) <= 0)
return 0;
return year >= 1920 && year <= 2002;
}
int validateIssueYear(char *content)
{
int year;
if (!isNumber(content))
return 0;
if (sscanf(content, "%d", &year) <= 0)
return 0;
return year >= 2010 && year <= 2020;
}
int validateExpirationYear(char *content)
{
int year;
if (!isNumber(content))
return 0;
if (sscanf(content, "%d", &year) <= 0)
return 0;
return year >= 2020 && year <= 2030;
}
int validateHeight(char *content)
{
int height;
if (strstr(content, "cm") != NULL)
return !(sscanf(content, "%dcm", &height) <= 0 || strlen(content) != 5 || !isNumberN(content, 3)) &&
height >= 150 && height <= 193;
if (strstr(content, "in") != NULL)
return !(sscanf(content, "%din", &height) <= 0 || strlen(content) != 4 || !isNumberN(content, 2)) &&
height >= 59 && height <= 76;
return 0;
}
int validateHairColor(char *content)
{
if (content[0] != '#')
return 0;
if (strlen(content) != 7)
return 0;
for (int i = 1; i < 7; i++)
if (!isDigit(content[i]) && (content[i] < 'a' || content[i] > 'f'))
return 0;
return 1;
}
int validateEyeColor(char *content)
{
char *valid[7] = {"amb", "blu", "brn", "gry", "grn", "hzl", "oth"};
if (strlen(content) != 3)
return 0;
for (int i = 0; i < 7; i++)
if (strstr(content, valid[i]) != NULL)
return 1;
return 0;
}
int validatePassportId(char *content)
{
if (strlen(content) != 9)
return 0;
return isNumber(content);
}
int validateCountry(char *content)
{
return 1;
}
int isNumber(char *content)
{
return isNumberN(content, strlen(content));
}
int isNumberN(char *content, int n)
{
for (int i = 0; i < n; i++)
if (!isDigit(content[i]))
return 0;
return 1;
}
int isDigit(char c)
{
return c >= '0' && c <= '9';
}