#include #include #include #define LINE_MAX_LENGTH 256 #define RED_CUBES 12 #define GREEN_CUBES 13 #define BLUE_CUBES 14 #define max(a, b) (a > b ? a : b) int main() { char *p, *buf, c; int sum = 0, id = 0, count = 0; unsigned int part2_sum = 0; buf = (char *)malloc(LINE_MAX_LENGTH); memset(buf, 0, LINE_MAX_LENGTH); p = buf; while ((c = getchar()) != EOF) { *p++ = c; if (c == '\n') { int possible = 1; int r = 0, g = 0, b = 0; p = buf; // Read Game ID sscanf(p, "Game %i", &id); // Move to char ':' while (*p++ != ':'); // Read draws for (;;) { sscanf(p, "%i %c", &count, &c); if ((c == 'r' && count > RED_CUBES) || (c == 'g' && count > GREEN_CUBES) || (c == 'b' && count > BLUE_CUBES)) { possible = 0; } switch (c) { case 'r': r = max(r, count); break; case 'g': g = max(g, count); break; case 'b': b = max(b, count); break; } // Read to next separator or end of line while (*p != '\n' && *p != ',' && *p != ';') p++; if (*p == '\n') { break; } p+=2; } if (possible) { sum += id; } part2_sum += r * g * b; memset(buf, 0, LINE_MAX_LENGTH); p = buf; } } free(buf); printf("%i\n", sum); printf("%u\n", part2_sum); }