Day 2 puzzle solve

This commit is contained in:
Benjamyn Love 2023-12-04 23:12:59 +11:00
parent a7baffd13e
commit f870bc5ff3

View File

@ -54,7 +54,7 @@ struct Round parse_round(char *line, struct Game game)
char *save_ptr; char *save_ptr;
char *token; char *token;
int cube_num; int cube_num;
struct Round round = {-1, -1, -1}; struct Round round = {0, 0, 0};
token = strtok_r(line, delim, &save_ptr); token = strtok_r(line, delim, &save_ptr);
while (token != NULL) while (token != NULL)
@ -79,7 +79,7 @@ struct Round parse_round(char *line, struct Game game)
return round; return round;
} }
struct Game parse_game_data(char *line, struct Game game) struct Game parse_game_data(char *line, struct Game game, int id)
{ {
// printf("%s\n", line); // printf("%s\n", line);
// https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm // https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
@ -88,6 +88,7 @@ struct Game parse_game_data(char *line, struct Game game)
char *save_ptr; char *save_ptr;
const char delim[2] = ";"; const char delim[2] = ";";
game.id = id;
token = strtok_r(line, delim, &save_ptr); token = strtok_r(line, delim, &save_ptr);
while (token != NULL) while (token != NULL)
{ {
@ -171,8 +172,8 @@ int main()
{ {
memcpy(tmpLine, &ptr[i], j); memcpy(tmpLine, &ptr[i], j);
// printf("ID: %i |", id); // printf("ID: %i |", id);
struct Game game = {id, -1}; struct Game game = {-1, -1};
games.games[games.iterator++] = parse_game_data(tmpLine, game); games.games[games.iterator++] = parse_game_data(tmpLine, game, id);
i += j; i += j;
break; break;
} }
@ -183,6 +184,27 @@ int main()
inLine = 0; inLine = 0;
} }
int total = 0;
for (int i = 0; i < games.iterator; i++) // Iterate through the games
{
int failed = 0;
for (int j = 0; j < games.iterator; j++) // Iterate thrugh the rounds
{
if (games.games[i].rounds[j].red > 12 || games.games[i].rounds[j].blue > 14 || games.games[i].rounds[j].green > 13)
{
failed = 1;
}
if (j == games.games[i].iterator)
{
break;
}
}
if (failed == 0)
{
total += games.games[i].id;
}
}
printf("\nThe total is: %i\n\n", total);
err = munmap(ptr, statbuf.st_size); err = munmap(ptr, statbuf.st_size);
if (err != 0) if (err != 0)
{ {