220 lines
5.0 KiB
C
220 lines
5.0 KiB
C
#include "libs/fileLoader.h"
|
|
#include "libs/strTools.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <math.h>
|
|
#include <time.h>
|
|
|
|
// 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);
|
|
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)
|
|
{
|
|
int num = (int)str - '0';
|
|
if (isdigit(num) == 0)
|
|
{
|
|
return num;
|
|
}
|
|
return -69420;
|
|
}
|
|
|
|
char *check_numbers_around(int x, int y, char chars[140][141], struct HashTable *table)
|
|
{
|
|
char *found_str;
|
|
// 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)
|
|
{
|
|
}
|
|
if (isdigit(num) == 0)
|
|
{
|
|
ht_insert(table, x, y, num);
|
|
}
|
|
if (1)
|
|
{
|
|
}
|
|
return found_str;
|
|
}
|
|
|
|
void check_symbols(int x, int y, char chars[140][141], struct HashTable *table)
|
|
{
|
|
char *symbols = "*$%&/@-=+#";
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
char *cur_char = (char *)(**chars * x) + y;
|
|
if (chars[x][y] == symbols[i] || chars[x][y] == '1')
|
|
{
|
|
|
|
check_numbers_around(x, y, chars, table);
|
|
// printf("\n\nGot a symbol: %c\n\n", chars[x][y]);
|
|
exit(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
struct HashTable *table = create_table(CAPACITY);
|
|
srand(time(NULL));
|
|
char *filename = "input";
|
|
struct OpenFile *o_file = load_file_to_mem(filename);
|
|
|
|
for (int i = 0; i < 50; i++)
|
|
{
|
|
for (int j = 0; j < 50; j++)
|
|
{
|
|
// printf("Adding %i, %i, %i\n", i, j, 243);
|
|
ht_insert(table, i, j, rand());
|
|
}
|
|
}
|
|
// 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");
|
|
// }
|
|
}
|
|
}
|
|
printf("\n");
|
|
return 0;
|
|
} |