aoc23/02/part1.c
2023-12-02 00:08:03 -08:00

43 lines
924 B
C

#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, cubes, sum;
sum = 0;
while (fgets(buf, MAX_LENGTH, stdin)) {
char* c = buf;
game_num = strtol(c + 5, &c, 10); // +5 skips the "Game "
while (*c != '\n') {
cubes = strtol(c + 2, &c, 10); // +2 skips ": ", ", ", or "; "
c++; // +1 skips the space
if ((*c == 'r' && cubes > 12) ||
(*c == 'g' && cubes > 13) ||
(*c == 'b' && cubes > 14)) {
game_num = 0;
}
c = skip_color(c);
}
sum += game_num;
}
printf("%d\n", sum);
return 0;
}