aoc-2023/c/02/part_2.c

77 lines
1.7 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;
int red = 0, green = 0, blue = 0;
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 > red) {
red = number;
}
} else if (parse_token(text, cursor, "green")) {
if (number > green) {
green = number;
}
} else if (parse_token(text, cursor, "blue")) {
if (number > blue) {
blue = number;
}
}
}
while (parse_token(text, cursor, ";") || parse_token(text, cursor, ","));
return red * green * blue;
}