Updoot
This commit is contained in:
parent
955d3c15fb
commit
1453d7ea21
@ -8,7 +8,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
// https://www.digitalocean.com/community/tutorials/hash-table-in-c-plus-plus
|
// https://www.digitalocean.com/community/tutorials/hash-table-in-c-plus-plus
|
||||||
#define CAPACITY 8000000 // Size of the hashtable
|
#define CAPACITY 100000 // Size of the hashtable
|
||||||
|
|
||||||
struct Ht_entry
|
struct Ht_entry
|
||||||
{
|
{
|
||||||
@ -156,19 +156,7 @@ int search_right(int x, int y, char chars[140][141])
|
|||||||
|
|
||||||
void get_string(int x, int y, char chars[140][141], struct HashTable *table)
|
void get_string(int x, int y, char chars[140][141], struct HashTable *table)
|
||||||
{
|
{
|
||||||
if (x < 0)
|
if (x < 0 || x > 140 || y < 0 || y > 141)
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (x > 140)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (y < 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (y > 141)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -273,7 +261,7 @@ int main()
|
|||||||
{
|
{
|
||||||
for (int j = 0; j < line_length; j++)
|
for (int j = 0; j < line_length; j++)
|
||||||
{
|
{
|
||||||
printf("%c", chars[i][j]);
|
// printf("%c", chars[i][j]);
|
||||||
check_symbols(i, j, chars, table);
|
check_symbols(i, j, chars, table);
|
||||||
|
|
||||||
// if (j % line_length == 0)
|
// if (j % line_length == 0)
|
||||||
|
|||||||
420
day3/puzzle2.c
Normal file
420
day3/puzzle2.c
Normal file
@ -0,0 +1,420 @@
|
|||||||
|
#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 5000000 // Size of the hashtable
|
||||||
|
// #define debug
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_string(int x, int y, char chars[140][141], struct HashTable *table)
|
||||||
|
{
|
||||||
|
if (x < 0 || x > 140 || y < 0 || y > 141)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
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, y - str_l_offset, retnum);
|
||||||
|
// tmpStr[0] = chars[x][y - str_l_offset]
|
||||||
|
// ht_insert(table, x, y, num);
|
||||||
|
return retnum;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_numbers_around(int x, int y, char chars[140][141], struct HashTable *table)
|
||||||
|
{
|
||||||
|
int nums[2] = {-1, -1};
|
||||||
|
int count = 0;
|
||||||
|
int tmpInt;
|
||||||
|
tmpInt = get_string(x - 1, y - 1, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpInt = get_string(x - 1, y, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpInt = get_string(x - 1, y + 1, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpInt = get_string(x, y - 1, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpInt = get_string(x, y + 1, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpInt = get_string(x + 1, y - 1, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpInt = get_string(x + 1, y, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpInt = get_string(x + 1, y + 1, chars, table);
|
||||||
|
if (tmpInt != -69420)
|
||||||
|
{
|
||||||
|
if ((nums[0] != -1 && nums[1] != -1) && (tmpInt == nums[0] || tmpInt == nums[1]))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (nums[0] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[0] = tmpInt;
|
||||||
|
}
|
||||||
|
else if (nums[1] == -1 && (tmpInt != nums[0] && tmpInt != nums[1]))
|
||||||
|
{
|
||||||
|
nums[1] = tmpInt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nums[0] != -1 && nums[1] != -1)
|
||||||
|
{
|
||||||
|
return (nums[0] * nums[1]);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_symbols(int x, int y, char chars[140][141], struct HashTable *table, int total)
|
||||||
|
{
|
||||||
|
// printf("x: %i, y: %i\n", x, y);
|
||||||
|
char symbol = '*';
|
||||||
|
int retNum = 0;
|
||||||
|
// char *cur_char = (char *)(**chars * x) + y;
|
||||||
|
// char *cur_char = (char *)malloc(1);
|
||||||
|
// cur_char[0] = chars[x][y];
|
||||||
|
if (chars[x][y] == symbol)
|
||||||
|
{
|
||||||
|
retNum = check_numbers_around(x, y, chars, table);
|
||||||
|
}
|
||||||
|
int newTotal = total + retNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
int total = 0;
|
||||||
|
for (int i = 0; i < line_count; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < line_length; j++)
|
||||||
|
{
|
||||||
|
#ifdef debug
|
||||||
|
printf("%c", chars[i][j]);
|
||||||
|
#endif
|
||||||
|
total = check_symbols(i, j, chars, table, total);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// for (int i = 0; i < table->size; i++)
|
||||||
|
// {
|
||||||
|
// if (table->entries[i])
|
||||||
|
// {
|
||||||
|
// total += table->entries[i]->num;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
printf("Total: %d\n", total);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user