#include #include #include #include #include #define GAME_OFFSET 5 enum color { RED, GREEN, BLUE, NUM_COLS }; char *parse_color(char *s, enum color *col) { if(*s == 'r') { *col = RED; return s + 3; } else if(*s == 'g') { *col = GREEN; return s + 5; } else if(*s == 'b') { *col = BLUE; return s + 4; } return NULL; } char *parse_round(char *s, int *r, int *g, int *b) { *r = *g = *b = 0; while(*s != ';' && *s != '\n') { if(*s == ',') s += 2; int cnt = strtol(s, &s, 10); s++; enum color col; s = parse_color(s, &col); switch(col) { break; case RED: *r = cnt; break; case GREEN: *g = cnt; break; case BLUE: *b = cnt; break; default: abort(); } } return s + 1; } char *parse_gid(char *l, int *gid) { *gid = strtol(l + GAME_OFFSET, &l, 10); return l + 2; } int main(void) { FILE *f = fopen("input", "r"); int sum = 0; char line[256]; while(fgets(line, sizeof line, f)) { int gid; int r_max = 0, g_max = 0, b_max = 0; for(char *p = parse_gid(line, &gid); *p;) { int r, g, b; p = parse_round(p, &r, &g, &b); if(r > r_max) r_max = r; if(g > g_max) g_max = g; if(b > b_max) b_max = b; } sum += r_max * g_max * b_max; } fclose(f); printf("%d\n", sum); return 0; }