diff --git a/snowflakes/TODO.md b/snowflakes/TODO.md new file mode 100644 index 0000000..909684a --- /dev/null +++ b/snowflakes/TODO.md @@ -0,0 +1,9 @@ +- demonstrate segfault when you iterate beyond the end of an array +- make sure `num_snowflakes` is on the stack, and that it goes out of scope after you return from get_num_snowflakes_from_stdin + - print the memory address, for example +- is snowflakes really empty after declaration? +- why do we have to create `snowflake` using malloc? its size is predictable at compile time, so why can't we use the stack? + +# Later +- how does `bool` work in C? The definitions are simple enough but what about the type? +- does clang put "0"s in a declared array? diff --git a/snowflakes/snowflakes b/snowflakes/snowflakes index 636d91c..0776123 100755 Binary files a/snowflakes/snowflakes and b/snowflakes/snowflakes differ diff --git a/snowflakes/snowflakes.c b/snowflakes/snowflakes.c index 61cfea5..519994d 100644 --- a/snowflakes/snowflakes.c +++ b/snowflakes/snowflakes.c @@ -1,13 +1,15 @@ #include #include +#include #define STRING_THERE_ARE_DUPLICATE_SNOWFLAKES "Twin snowflakes found.\n" #define STRING_SNOWFLAKES_ARE_UNIQUE "No two snowflakes are alike.\n" #define LEN_SNOWFLAKE 6 +#define LARGEST_POSSIBLE_SUM 6000 int get_num_snowflakes_from_stdin(void) { - int num_snowflakes; + int num_snowflakes; // this is allocated on the stack scanf("%d", &num_snowflakes); return num_snowflakes; } @@ -58,11 +60,36 @@ bool they_are_equal(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE]) return false; } -void populate_snowflakes_from_stdin(int snowflakes[][LEN_SNOWFLAKE], int num_snowflakes) +int get_hash(int snowflake[LEN_SNOWFLAKE]) { - for (int i = 0; i < num_snowflakes; i++) + int sum; + for (int i = 0; i < LEN_SNOWFLAKE; i++) + { + sum += snowflake[i]; + } + return sum; +} + +typedef struct Entry { + int snowflake[LEN_SNOWFLAKE]; + struct Entry * next; +} Entry; + +void populate_snowflakes_from_stdin(Entry * snowflakes[LARGEST_POSSIBLE_SUM], int num_snowflakes) +{ + int hash; + Entry * snowflake; + for (int _ = 0; _ < num_snowflakes; _++) + { + snowflake = malloc(sizeof(Entry)); for (int j = 0; j < LEN_SNOWFLAKE; j++) - scanf("%d", &snowflakes[i][j]); + { + scanf("%d", &snowflake->snowflake[j]); + } + hash = get_hash(snowflake->snowflake); + snowflake->next = snowflakes[hash]; + snowflakes[hash] = snowflake; + } } int main(void) @@ -71,17 +98,17 @@ int main(void) // - the tips add up to the same amount // - try to skip any array (hash map) slots with one or fewer elements stored int num_snowflakes = get_num_snowflakes_from_stdin(); - int snowflakes[num_snowflakes][LEN_SNOWFLAKE]; + Entry * snowflakes[LARGEST_POSSIBLE_SUM]; // 48MB of memory, 6 million slots populate_snowflakes_from_stdin(snowflakes, num_snowflakes); - for (int i = 0; i < num_snowflakes; i++) - for (int j = i + 1; j < num_snowflakes; j++) - if (they_are_equal(snowflakes[i], snowflakes[j])) - { - printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES); - return 0; - } + // for (int i = 0; i < num_snowflakes; i++) + // for (int j = i + 1; j < num_snowflakes; j++) + // if (they_are_equal(snowflakes[i], snowflakes[j])) + // { + // printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES); + // return 0; + // } - printf(STRING_SNOWFLAKES_ARE_UNIQUE); + // printf(STRING_SNOWFLAKES_ARE_UNIQUE); return 0; } diff --git a/snowflakes/snowflakes.txt b/snowflakes/snowflakes.txt index 8a7cdaf..b361343 100644 --- a/snowflakes/snowflakes.txt +++ b/snowflakes/snowflakes.txt @@ -1,5 +1,7 @@ 4 6 5 4 3 2 1 +4 6 5 1 2 3 +1 2 10 2 1 5 15 12 12 12 12 12 4 5 6 1 2 3 1 1 1 1 1 1