1.c (1805B)
1 #include "../../util.h" 2 3 #include <stdio.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <assert.h> 7 #include <stdbool.h> 8 #include <ctype.h> 9 10 typedef struct { 11 int id; 12 int colors[3]; 13 } game; 14 15 int main() { 16 FILE *inputfile = fopen("./input", "r"); 17 char line[1024] = { 0 }; 18 int sum = 0; 19 size_t len = 0; 20 ssize_t read; 21 if (inputfile == NULL) { 22 printf("Input file not found. Exiting."); 23 exit(1); 24 } 25 while (fgets(line, 1024, inputfile)) { 26 int colors[3] = { 0, 0, 0 }; 27 line[strcspn(line, "\n")] = 0; 28 char **splitted = str_split(line, ':'); 29 char *id = str_split(splitted[0], ' ')[1]; 30 char **sets = str_split(splitted[1], ';'); 31 bool ok = true; 32 for (int i = 0; sets[i]; i++) { 33 int colors[3] = { 0, 0, 0 }; 34 sets[i] = trimwhitespace(sets[i]); 35 char **subset = str_split(sets[i], ','); 36 for (int j = 0; subset[j]; j++) { 37 subset[j] = trimwhitespace(subset[j]); 38 char **splittedsubset = str_split(subset[j], ' '); 39 char *count = splittedsubset[0]; 40 char *color = splittedsubset[1]; 41 if (strncmp(color, "red", 3) == 0) { 42 colors[0] = atoi(count); 43 } else if (strncmp(color, "green", 5) == 0) { 44 colors[1] = atoi(count); 45 } else if (strncmp(color, "blue", 4) == 0) { 46 colors[2] = atoi(count); 47 } 48 } 49 if (colors[0] > 12 || colors[1] > 13 || colors[2]> 14) { 50 ok = false; 51 } 52 } 53 if (ok) { 54 sum += atoi(id); 55 } 56 free(splitted); 57 } 58 fclose(inputfile); 59 printf("%d\n", sum); 60 return 0; 61 }