#include "libs/fileLoader.h" #include "libs/strTools.h" #include #include #include #include #include #include // https://www.digitalocean.com/community/tutorials/hash-table-in-c-plus-plus #define CAPACITY 8000000 // Size of the hashtable struct Ht_entry { int num; }; struct HashTable { struct Ht_entry **entries; int size; int count; }; unsigned long hash_function(int x, int y, int num) { unsigned long i = 0; // i = (((x + 1) * num) - y); i = (69420 * x) - y ^ 2; return i % CAPACITY; } struct Ht_entry *create_item(int x, int y, int num) { // Creates a pointer to the a new HashTable item struct Ht_entry *entry = (struct Ht_entry *)malloc(sizeof(struct Ht_entry)); entry->num = (int)malloc(sizeof(num)); // int nDigits = floor(log10(abs(num))) + 1; entry->num = num; return entry; } void free_entry(struct Ht_entry *entry) { free(entry->num); free(entry); } void free_table(struct HashTable *table) { for (int i = 0; i < table->size; i++) { struct Ht_entry *entry = table->entries[i]; if (table != NULL) { free_entry(entry); } } free(table->entries); free(table); } void ht_insert(struct HashTable *table, int x, int y, int num) { struct Ht_entry *entry = create_item(x, y, num); int index = hash_function(x, y, num); struct Ht_entry *current_item = table->entries[index]; if (current_item == NULL) { if (table->count == table->size) { printf("Hash table full ;|\n"); free_entry(entry); return; } table->entries[index] = entry; table->count++; } else { // printf("Collision at %i, %i, %i, %i\n", index, x, y, num); // printf("HASH COLISION, MAYDAY MAYDAY!\n"); // exit(2); } } struct HashTable *create_table(int size) { struct HashTable *table = (struct HashTable *)malloc(sizeof(struct HashTable)); table->size = size; table->count = 0; table->entries = (struct Ht_entry **)calloc(table->size, sizeof(struct Ht_entry *)); for (int i = 0; i < table->size; i++) { table->entries[i] = NULL; } return table; } int ht_search(struct HashTable *table, int x, int y, int num) { int index = hash_function(x, y, num); struct Ht_entry *entry = table->entries[index]; if (entry != NULL) { return entry->num; } return NULL; } int simple_int_conv(char str) { char *tmp; char *fuckC = (char *)malloc(1); fuckC[0] = str; int num = strtol(fuckC, &tmp, 10); if (tmp == fuckC) { return -69420; } else { return num; } } int search_left(int x, int y, char chars[140][141]) { for (int i = 0; i < __INT64_MAX__; i++) { if (chars[x][y - i] == '.' || chars[x][y - i] == '*' || chars[x][y - i] == '$' || chars[x][y - i] == '%' || chars[x][y - i] == '&' || chars[x][y - i] == '/' || chars[x][y - i] == '@' || chars[x][y - i] == '-' || chars[x][y - i] == '=' || chars[x][y - i] == '+' || chars[x][y - i] == '#') { return i - 1; } } } int search_right(int x, int y, char chars[140][141]) { for (int i = 0; i < __INT64_MAX__; i++) { if (chars[x][y + i] == '.' || chars[x][y + i] == '*' || chars[x][y + i] == '$' || chars[x][y + i] == '%' || chars[x][y + i] == '&' || chars[x][y + i] == '/' || chars[x][y + i] == '@' || chars[x][y + i] == '-' || chars[x][y + i] == '=' || chars[x][y + i] == '+' || chars[x][y + i] == '#') { return i; } } } void get_string(int x, int y, char chars[140][141], struct HashTable *table) { // printf("%c", chars[x][y]); // char *cur_char = (char *)chars[x][y]; // long int num = char_to_int(cur_char); int num = simple_int_conv(chars[x][y]); if (num == -69420 || num < 0) { return; } if (isdigit(num) == 0) { int str_l_offset = search_left(x, y, chars); int str_r_offset = search_right(x, y, chars); // printf("%i - %i\n", str_l_offset, str_r_offset); char *tmpStr = malloc(5); int iter = 0; for (int i = str_l_offset; i > 0; i--) { tmpStr[iter] = chars[x][y - i]; iter++; } for (int i = 0; i < str_r_offset; i++) { tmpStr[iter] = chars[x][y + i]; iter++; } // printf("str: %s\n", tmpStr); int retnum = char_to_int(tmpStr); // printf("int: %i\n", retnum); ht_insert(table, x - str_l_offset, y, retnum); // tmpStr[0] = chars[x][y - str_l_offset] // ht_insert(table, x, y, num); } return; } void check_numbers_around(int x, int y, char chars[140][141], struct HashTable *table) { get_string(x - 1, y - 1, chars, table); get_string(x - 1, y, chars, table); get_string(x - 1, y + 1, chars, table); get_string(x, y - 1, chars, table); get_string(x, y + 1, chars, table); get_string(x + 1, y - 1, chars, table); get_string(x + 1, y, chars, table); get_string(x + 1, y + 1, chars, table); } void check_symbols(int x, int y, char chars[140][141], struct HashTable *table) { // printf("x: %i, y: %i\n", x, y); char *symbols = "*$%&/@-=+#"; for (int i = 0; i < 10; i++) { // char *cur_char = (char *)(**chars * x) + y; // char *cur_char = (char *)malloc(1); // cur_char[0] = chars[x][y]; if (chars[x][y] == symbols[i]) { check_numbers_around(x, y, chars, table); } } } int main() { struct HashTable *table = create_table(CAPACITY); srand(time(NULL)); char *filename = "input"; struct OpenFile *o_file = load_file_to_mem(filename); // Load the dataset into memory so we have the full data mapped // in an easy to work with way const int line_length = 141; const int line_count = 140; char chars[line_count][line_length]; int iter = 0; for (int i = 0; i < o_file->fileSize; i++) { chars[iter][i % line_length] = o_file->fileData[i]; if (o_file->fileData[i] == '\n') { iter++; } // printf("%c", o_file->fileData[i]); } // Look through the data to find all the symbols // Once we find a symbol look in the spots around // the symbol to find any numbers, if we find a // number then we look for the start point and // the end point -> convert the string to an int // Once we have the number we store it in a hashmap // Using the X/Y coordinates of the char at the start // point plus the int value if the char... I hope for (int i = 0; i < line_count; i++) { for (int j = 0; j < line_length; j++) { // printf("%c", chars[i][j]); check_symbols(i, j, chars, table); // if (j % line_length == 0) // { // printf("\n"); // } } } int total = 0; for (int i = 0; i < table->size; i++) { if (table->entries[i]) { total += table->entries[i]->num; } } printf("\n"); printf("Total: %i\n", total); return 0; }