aoc23/02/part1.c

46 lines
1.2 KiB
C
Raw Normal View History

2023-12-02 02:01:25 -06:00
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 165
#define RED 1
#define GREEN 2
#define BLUE 3
int strtocolor(char* c, char** end) {
if (c[0] == 'r' && c[1] == 'e' && c[2] == 'd') {
*end = c + 3;
return RED;
} else if (c[0] == 'g' && c[1] == 'r' && c[2] == 'e' && c[3] == 'e' && c[4] == 'n') {
*end = c + 5;
return GREEN;
} else if (c[0] == 'b' && c[1] == 'l' && c[2] == 'u' && c[3] == 'e') {
*end = c + 4;
return BLUE;
}
abort();
}
int main(void) {
char buf[MAX_LENGTH];
int game_num, color_num, color, 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') {
color_num = strtol(c + 2, &c, 10); // +2 skips ": ", ", ", or "; "
color = strtocolor(c + 1, &c); // +1 skips the space
if ((color == RED && color_num > 12) ||
(color == GREEN && color_num > 13) ||
(color == BLUE && color_num > 14)) {
game_num = 0;
}
}
sum += game_num;
}
printf("%d\n", sum);
return 0;
}