aoc23/02/part2.c

29 lines
844 B
C
Raw Normal View History

2023-12-03 03:12:13 -06:00
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 165
int main(void) {
char buf[MAX_LENGTH];
int color_num, sum, max_r, max_g, max_b;
sum = 0;
while (fgets(buf, MAX_LENGTH, stdin)) {
char* c = buf + 5; // +5 skips the "Game "
for (; *c != ':'; c++);
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;
for (; *c >= 'a' && *c <= 'z'; c++);
}
sum += max_r * max_g * max_b;
}
printf("%d\n", sum);
return 0;
}