Crying rn 😢

This commit is contained in:
Benjamyn Love 2023-12-04 21:57:11 +11:00
parent 9c7c9012a0
commit 59e848e399

View File

@ -16,25 +16,18 @@ struct Game
int blue; int blue;
}; };
struct Games struct Round
{ {
struct Game games[100]; struct Game games[100];
int interator; int iterator;
int id;
}; };
void parse_game_data(char *line, struct Games games) struct Games
{ {
// printf("%s\n", line); struct Round rounds[10];
char *token; int interator;
const char delim[2] = ";"; };
token = strtok(line, delim);
printf("First token is: %s\n", token);
char *token2;
const char delim2[2] = ",";
token2 = strtok(token, delim2);
printf("Second token is: %s\n", token2);
struct Game tmpGame;
}
int str_to_int(char *str) int str_to_int(char *str)
{ {
@ -44,6 +37,89 @@ int str_to_int(char *str)
return result; return result;
} }
char *parse_cube(char *line, int *cube_num)
{
const char delim[2] = " ";
char *save_ptr;
char *token;
token = strtok_r(line, delim, &save_ptr);
*cube_num = str_to_int(token);
token = strtok_r(NULL, delim, &save_ptr);
return token; // Cube colour
}
struct Round parse_round(char *line, int id)
{
const char delim[2] = ",";
char *save_ptr;
char *token;
int cube_num;
struct Round round;
struct Game game = {-1, -1, -1};
round.id = id;
round.iterator = 0;
token = strtok_r(line, delim, &save_ptr);
while (token != NULL)
{
char *colour = parse_cube(token, &cube_num);
if (!strcmp(colour, "red"))
{
game.red = cube_num;
}
else if (!strcmp(colour, "blue"))
{
game.blue = cube_num;
}
else if (!strcmp(colour, "green"))
{
game.green = cube_num;
}
token = strtok_r(NULL, delim, &save_ptr);
round.games[round.iterator++] = game;
}
return round;
}
void parse_game_data(char *line, struct Games games, int id)
{
// printf("%s\n", line);
// https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
// https://www.tutorialspoint.com/data_structures_algorithms/hash_table_program_in_c.htm
char *token;
char *save_ptr;
const char delim[2] = ";";
token = strtok_r(line, delim, &save_ptr);
while (token != NULL)
{
games.rounds[games.interator++] = parse_round(token, id);
token = strtok_r(NULL, delim, &save_ptr);
}
// while (token != NULL)
// {
// printf("%s\n", token);
// char *inner_orig_line = malloc(255);
// memcpy(inner_orig_line, token, 255);
// token2 = strtok(orig_line, delim2);
// while (token2 != NULL)
// {
// printf("%s\n", token2);
// token3 = strtok(token2, delim3);
// int count = str_to_int(token3);
// char *colour = strtok(NULL, delim3);
// //
// printf("We have %i %s cubes\n", count, colour);
// token2 = strtok(NULL, delim2);
// }
// token = strtok(NULL, delim);
// }
// token2 = strtok(token, delim2);
// printf("Second token is: %s\n", token2);
struct Game tmpGame;
}
int get_id(char *ptr) int get_id(char *ptr)
{ {
int id; int id;
@ -56,7 +132,7 @@ int get_id(char *ptr)
} }
else else
{ {
ID_OFFSET = i; ID_OFFSET = i;
break; break;
} }
} }
@ -67,6 +143,7 @@ int get_id(char *ptr)
int main() int main()
{ {
struct Games games; struct Games games;
games.interator = 0;
int MAX_RED = 12; int MAX_RED = 12;
int MAX_GREEN = 13; int MAX_GREEN = 13;
int MAX_BLUE = 14; int MAX_BLUE = 14;
@ -109,14 +186,15 @@ int main()
inLine = 1; inLine = 1;
i += GAME_OFFSET; i += GAME_OFFSET;
id = get_id(&ptr[i]); id = get_id(&ptr[i]);
i += ID_OFFSET + 1; i += ID_OFFSET + 1;
continue; continue;
} }
if (ptr[i + j] == '\n') if (ptr[i + j] == '\n')
{ {
memcpy(tmpLine, &ptr[i], j); memcpy(tmpLine, &ptr[i], j);
printf("ID: %i |", id); // printf("ID: %i |", id);
parse_game_data(tmpLine, games); parse_game_data(tmpLine, games, id);
exit(1);
i += j; i += j;
break; break;
} }