#include #include #include #include "libs/fileLoader.h" #include "libs/strTools.h" #define MAX_STR_LEN 255 #define LineCount 7 #define OUR_LEN 5 #define WIN_LEN 8 // defaults // #define LineCount 219 // #define OUR_LEN 10 // #define WIN_LEN 25 struct ScratchCards { int cards[LineCount]; }; struct Game { int ourNumbers[OUR_LEN]; int ourIterator; int winNumbers[WIN_LEN]; int winIterator; int gamesWon; int game_id; }; struct Games { struct Game games[LineCount]; int iterator; }; struct Game *lineParser(char *line) // , struct Games *games { struct Game *game = malloc(sizeof(struct Game)); game->ourIterator = 0; game->winIterator = 0; game->gamesWon = 0; // Returns the total points of the line // Line has // Card 1: 99 65 21 4 72 20 77 98 27 70 | 34 84 74 18 41 45 72 2 1 75 52 47 50 93 25 10 79 87 42 69 8 12 54 96 92 // First split by ':' so we get the 'Card | ID' && 'Game Numbers | Winning Numbers' // We skip the first token as we do not care about the game ID's in this case // We then split the game data by '|' and then split each token by ' ' and parse the integers and then store then in arrays // for doing the actual challenge int total; // Parser system (oh my lord) char *token; char *savePtr; char delim[2] = ":"; char *idToken; char *idSavePtr; char idDelim[2] = " "; char *gameToken; char *gameSavePtr; char gameDelim[2] = "|"; char *gameIntToken; char *gameIntSavePtr; char gameIntDelim[2] = " "; token = strtok_r(line, delim, &savePtr); idToken = strtok_r(token, idDelim, &idSavePtr); idToken = strtok_r(NULL, idDelim, &idSavePtr); game->game_id = char_to_int(idToken); token = strtok_r(NULL, delim, &savePtr); gameToken = strtok_r(token, gameDelim, &gameSavePtr); gameIntToken = strtok_r(gameToken, gameIntDelim, &gameIntSavePtr); while (gameIntToken != NULL) { game->ourNumbers[game->ourIterator++] = char_to_int(gameIntToken); gameIntToken = strtok_r(NULL, gameIntDelim, &gameIntSavePtr); } gameToken = strtok_r(NULL, gameDelim, &gameSavePtr); gameIntToken = strtok_r(gameToken, gameIntDelim, &gameIntSavePtr); while (gameIntToken != NULL) { game->winNumbers[game->winIterator++] = char_to_int(gameIntToken); gameIntToken = strtok_r(NULL, gameIntDelim, &gameIntSavePtr); } // End parser, results are in ourNumbers[] and winNumbers[] for (int i = 0; i < game->ourIterator; i++) { for (int j = 0; j < game->winIterator; j++) { if (game->ourNumbers[i] == game->winNumbers[j]) { game->gamesWon++; break; } } } // int tmpTotal = 0; // This is now the count of the cards // for (int i = 0; i < game->ourIterator; i++) // { // if (game->winners[i] == 1) // { // if (tmpTotal == 0) // { // tmpTotal = 1; // continue; // } // tmpTotal *= 2; // } // } return game; } int main() { char *filename = "input_test"; struct OpenFile *o_file = load_file_to_mem(filename); int total = 0; // Load the input file into my structs struct Games *games = malloc(sizeof(struct Games)); games->iterator = 0; // char *line = (char *)malloc(MAX_STR_LEN); // Start new line variable? for (int i = 0; i < o_file->fileSize; i++) { char *line = (char *)malloc(MAX_STR_LEN); for (int j = 0; j < MAX_STR_LEN; j++) { line[j] = (char)o_file->fileData[i + j]; if (o_file->fileData[i + j] == '\n') { games->games[games->iterator++] = *lineParser(line); free(line); i += j; break; } } } // Finish loading shit // This is not counting the games correctly and I don't know why // Start doing the big recursive loop struct ScratchCards *cards = malloc(sizeof(struct ScratchCards)); // Set initial card count to 1 for (int i = 0; i < games->iterator; i++) { cards->cards[i] = 1; } for (int gameId = 0; gameId <= LineCount; gameId++) { if (gameId == games->iterator) { break; } // cards->cards[gameId] += 1; Uneeded as we default them to 1 games won int winningGames = games->games[gameId].gamesWon; int iterations = winningGames + cards->cards[gameId]; for (int i = 1; i <= iterations; i++) { if (cards->cards[gameId + i] < LineCount - 1) { cards->cards[gameId + i]++; } } // int winningGames = games->games[gameId].gamesWon; // for (int extraId = gameId; extraId < (gameId + winningGames); extraId++) // { // cards->cards[extraId + 1]++; // } } // Finish sad loop and cry for (int i = 0; i < LineCount; i++) { total += cards->cards[i]; } printf("Total is: %i\n", total); return 0; } /* char *token; char *save_ptr; const char delim[2] = ";"; game.id = id; token = strtok_r(line, delim, &save_ptr); while (token != NULL) { game.rounds[game.iterator++] = parse_round(token, game); token = strtok_r(NULL, delim, &save_ptr); } */