137 lines
3.5 KiB
C
137 lines
3.5 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "libs/fileLoader.h"
|
|
#include "libs/strTools.h"
|
|
|
|
#define MAX_STR_LEN 255
|
|
|
|
int lineParser(char *line)
|
|
{
|
|
// 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 *gameToken;
|
|
char *gameSavePtr;
|
|
char gameDelim[2] = "|";
|
|
|
|
char *gameIntToken;
|
|
char *gameIntSavePtr;
|
|
char gameIntDelim[2] = " ";
|
|
|
|
int ourNumbers[10];
|
|
int ourIterator = 0;
|
|
int winNumbers[25];
|
|
int winIterator = 0;
|
|
|
|
token = strtok_r(line, delim, &savePtr);
|
|
token = strtok_r(NULL, delim, &savePtr); // Go to the next token
|
|
|
|
gameToken = strtok_r(token, gameDelim, &gameSavePtr);
|
|
gameIntToken = strtok_r(gameToken, gameIntDelim, &gameIntSavePtr);
|
|
while (gameIntToken != NULL)
|
|
{
|
|
ourNumbers[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)
|
|
{
|
|
winNumbers[winIterator++] = char_to_int(gameIntToken);
|
|
gameIntToken = strtok_r(NULL, gameIntDelim, &gameIntSavePtr);
|
|
}
|
|
// End parser, results are in ourNumbers[] and winNumbers[]
|
|
|
|
int lastNumWin = 0;
|
|
int winners[ourIterator];
|
|
|
|
for (int i = 0; i < ourIterator; i++)
|
|
{
|
|
for (int j = 0; j < winIterator; j++)
|
|
{
|
|
if (ourNumbers[i] == winNumbers[j])
|
|
{
|
|
winners[i] = 1;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
winners[i] = 0;
|
|
}
|
|
}
|
|
}
|
|
int tmpTotal = 0;
|
|
for (int i = 0; i < ourIterator; i++)
|
|
{
|
|
if (winners[i] == 1)
|
|
{
|
|
if (tmpTotal == 0)
|
|
{
|
|
tmpTotal = 1;
|
|
continue;
|
|
}
|
|
tmpTotal *= 2;
|
|
}
|
|
}
|
|
return tmpTotal;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
char *filename = "input";
|
|
struct OpenFile *o_file = load_file_to_mem(filename);
|
|
|
|
int total = 0;
|
|
int inLine = 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')
|
|
{
|
|
int ret = lineParser(line);
|
|
total += ret;
|
|
free(line);
|
|
i += j;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
printf("Total is: %d\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);
|
|
}
|
|
*/ |