76 lines
1.6 KiB
C
76 lines
1.6 KiB
C
#include <cs50.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <parse.h>
|
|
|
|
// 1957 is too low
|
|
|
|
// The amount of games expected
|
|
const int games = 100;
|
|
|
|
// How many of each cube are allowed
|
|
const int allowed_red = 12;
|
|
const int allowed_green = 13;
|
|
const int allowed_blue = 14;
|
|
|
|
// How many letter each color takes up
|
|
const int chars_red = strlen("red");
|
|
const int chars_green = strlen("green");
|
|
const int chars_blue = strlen("blue");
|
|
|
|
// Pre-defined functions
|
|
int game_score(char *text);
|
|
|
|
int main(void) {
|
|
int sum = 0;
|
|
|
|
char *inputs[games];
|
|
for (int i = 0; i < games; i++) {
|
|
inputs[i] = get_string("");
|
|
}
|
|
|
|
for (int i = 0; i < games; i++) {
|
|
sum += game_score(inputs[i]);
|
|
}
|
|
|
|
printf("%i\n", sum);
|
|
return 0;
|
|
}
|
|
|
|
/* Determine a game's score.
|
|
*/
|
|
int game_score(char *text) {
|
|
int value = 0;
|
|
int game;
|
|
int *cursor = &value;
|
|
|
|
parse_token(text, cursor, "Game ");
|
|
parse_int(text, cursor, &game);
|
|
parse_token(text, cursor, ":");
|
|
|
|
do
|
|
{
|
|
int number;
|
|
|
|
parse_token(text, cursor, " ");
|
|
parse_int(text, cursor, &number);
|
|
parse_token(text, cursor, " ");
|
|
|
|
if (parse_token(text, cursor, "red")) {
|
|
if (number > allowed_red) {
|
|
return 0;
|
|
}
|
|
} else if (parse_token(text, cursor, "green")) {
|
|
if (number > allowed_green) {
|
|
return 0;
|
|
}
|
|
} else if (parse_token(text, cursor, "blue")) {
|
|
if (number > allowed_blue) {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
while (parse_token(text, cursor, ";") || parse_token(text, cursor, ","));
|
|
|
|
return game;
|
|
} |