#include #include #define MAX_LENGTH 165 char* skip_color(char* c) { switch (*c) { case 'r': return c + 3; case 'g': return c + 5; case 'b': return c + 4; } abort(); } int main(void) { char buf[MAX_LENGTH]; int game_num, color_num, sum, max_r, max_g, max_b; sum = 0; while (fgets(buf, MAX_LENGTH, stdin)) { char* c = buf; game_num = strtol(c + 5, &c, 10); // +5 skips the "Game " max_r = max_g = max_b = 0; while (*c != '\n') { color_num = strtol(c + 2, &c, 10); // +2 skips ": ", ", ", or "; " c++; // +1 skips the space max_r = (*c == 'r' && color_num > max_r) ? color_num : max_r; max_g = (*c == 'g' && color_num > max_g) ? color_num : max_g; max_b = (*c == 'b' && color_num > max_b) ? color_num : max_b; c = skip_color(c); } sum += max_r * max_g * max_b; } printf("%d\n", sum); return 0; }