aoc23/02/part1.c

43 lines
924 B
C
Raw Normal View History

2023-12-02 02:01:25 -06:00
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 165
2023-12-02 02:08:03 -06:00
char* skip_color(char* c) {
switch (*c) {
case 'r':
return c + 3;
case 'g':
return c + 5;
case 'b':
return c + 4;
2023-12-02 02:01:25 -06:00
}
abort();
}
2023-12-02 02:08:03 -06:00
2023-12-02 02:01:25 -06:00
int main(void) {
char buf[MAX_LENGTH];
2023-12-02 02:08:03 -06:00
int game_num, cubes, sum;
2023-12-02 02:01:25 -06:00
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') {
2023-12-02 02:08:03 -06:00
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)) {
2023-12-02 02:01:25 -06:00
game_num = 0;
}
2023-12-02 02:08:03 -06:00
c = skip_color(c);
2023-12-02 02:01:25 -06:00
}
sum += game_num;
}
printf("%d\n", sum);
return 0;
}