diff --git a/day3/puzzle1.c b/day3/puzzle1.c index 7e26fbf..b36f172 100644 --- a/day3/puzzle1.c +++ b/day3/puzzle1.c @@ -27,7 +27,8 @@ hash_function(int x, int y, int num) { unsigned long i = 0; - i = (((x + 1) * num) - y); + // i = (((x + 1) * num) - y); + i = (69420 * x) - y ^ 2; return i % CAPACITY; } @@ -82,7 +83,7 @@ void ht_insert(struct HashTable *table, int x, int y, int num) } else { - printf("Collision at %i, %i, %i, %i\n", index, x, y, num); + // printf("Collision at %i, %i, %i, %i\n", index, x, y, num); // printf("HASH COLISION, MAYDAY MAYDAY!\n"); // exit(2); } @@ -117,46 +118,104 @@ int ht_search(struct HashTable *table, int x, int y, int num) int simple_int_conv(char str) { - int num = (int)str - '0'; - if (isdigit(num) == 0) + char *tmp; + char *fuckC = (char *)malloc(1); + fuckC[0] = str; + int num = strtol(fuckC, &tmp, 10); + if (tmp == fuckC) + { + return -69420; + } + else { return num; } - return -69420; } -char *check_numbers_around(int x, int y, char chars[140][141], struct HashTable *table) +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) { - 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 (num == -69420 || num < 0) { + return; } if (isdigit(num) == 0) { - ht_insert(table, x, y, num); + 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); } - if (1) - { - } - return found_str; + 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; - if (chars[x][y] == symbols[i] || chars[x][y] == '1') + // 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); - // printf("\n\nGot a symbol: %c\n\n", chars[x][y]); - exit(0); } } } @@ -168,14 +227,6 @@ int main() 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; @@ -215,6 +266,18 @@ int main() // } } } + + 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; } \ No newline at end of file diff --git a/libs/strTools.c b/libs/strTools.c index 6c611c4..2a886ba 100644 --- a/libs/strTools.c +++ b/libs/strTools.c @@ -9,6 +9,5 @@ int char_to_int(char *str) int result; char *tmp; result = strtol(str, &tmp, 10); - printf("%i", errno); return result; } \ No newline at end of file