210 lines
5.4 KiB
C
210 lines
5.4 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "libs/fileLoader.h"
|
|
#include "libs/strTools.h"
|
|
|
|
#define testing
|
|
|
|
#define MAX_STR_LEN 255
|
|
#ifdef testing
|
|
#define LineCount 7
|
|
#define OUR_LEN 5
|
|
#define WIN_LEN 8
|
|
#else
|
|
// defaults
|
|
#define LineCount 219
|
|
#define OUR_LEN 10
|
|
#define WIN_LEN 25
|
|
#endif
|
|
struct ScratchCards
|
|
{
|
|
int cards[LineCount];
|
|
};
|
|
|
|
typedef struct Game
|
|
{
|
|
int ourNumbers[OUR_LEN];
|
|
int ourIterator;
|
|
int winNumbers[WIN_LEN];
|
|
int winIterator;
|
|
int gamesWon;
|
|
|
|
int game_id;
|
|
} Game;
|
|
|
|
typedef struct Games
|
|
{
|
|
Game games[LineCount];
|
|
int iterator;
|
|
} Games;
|
|
|
|
Game *lineParser(char *line) // , Games *games
|
|
{
|
|
Game *game = malloc(sizeof(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
|
|
Games *games = malloc(sizeof(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 - 1; 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 = 0; i <= iterations; i++)
|
|
{
|
|
if (gameId + (i + 1) <= LineCount)
|
|
{
|
|
cards->cards[gameId + (i + 1)]++;
|
|
}
|
|
}
|
|
// 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);
|
|
}
|
|
*/ |