aoc23/02/part2.c

41 lines
1.0 KiB
C
Raw Normal View History

2023-12-02 02:01:25 -06:00
#include <stdio.h>
#include <stdlib.h>
#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;
}