From f870bc5ff36ded341c6933a9403870b62a64d2bb Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Mon, 4 Dec 2023 23:12:59 +1100 Subject: [PATCH] Day 2 puzzle solve --- day2/puzzle1/main.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/day2/puzzle1/main.c b/day2/puzzle1/main.c index 9c0bfe3..a8055a3 100644 --- a/day2/puzzle1/main.c +++ b/day2/puzzle1/main.c @@ -54,7 +54,7 @@ struct Round parse_round(char *line, struct Game game) char *save_ptr; char *token; int cube_num; - struct Round round = {-1, -1, -1}; + struct Round round = {0, 0, 0}; token = strtok_r(line, delim, &save_ptr); while (token != NULL) @@ -79,7 +79,7 @@ struct Round parse_round(char *line, struct Game game) 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); // 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; const char delim[2] = ";"; + game.id = id; token = strtok_r(line, delim, &save_ptr); while (token != NULL) { @@ -171,8 +172,8 @@ int main() { memcpy(tmpLine, &ptr[i], j); // printf("ID: %i |", id); - struct Game game = {id, -1}; - games.games[games.iterator++] = parse_game_data(tmpLine, game); + struct Game game = {-1, -1}; + games.games[games.iterator++] = parse_game_data(tmpLine, game, id); i += j; break; } @@ -183,6 +184,27 @@ int main() 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); if (err != 0) {