Day 2 puzzle solve
This commit is contained in:
parent
a7baffd13e
commit
f870bc5ff3
@ -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)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user