This commit is contained in:
2023-11-30 13:47:21 +01:00
parent 19741b87d6
commit 1dd443cbba
4 changed files with 51 additions and 13 deletions

9
snowflakes/TODO.md Normal file
View File

@@ -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?

Binary file not shown.

View File

@@ -1,13 +1,15 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#define STRING_THERE_ARE_DUPLICATE_SNOWFLAKES "Twin snowflakes found.\n" #define STRING_THERE_ARE_DUPLICATE_SNOWFLAKES "Twin snowflakes found.\n"
#define STRING_SNOWFLAKES_ARE_UNIQUE "No two snowflakes are alike.\n" #define STRING_SNOWFLAKES_ARE_UNIQUE "No two snowflakes are alike.\n"
#define LEN_SNOWFLAKE 6 #define LEN_SNOWFLAKE 6
#define LARGEST_POSSIBLE_SUM 6000
int get_num_snowflakes_from_stdin(void) int get_num_snowflakes_from_stdin(void)
{ {
int num_snowflakes; int num_snowflakes; // this is allocated on the stack
scanf("%d", &num_snowflakes); scanf("%d", &num_snowflakes);
return num_snowflakes; return num_snowflakes;
} }
@@ -58,11 +60,36 @@ bool they_are_equal(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE])
return false; 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++) 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) int main(void)
@@ -71,17 +98,17 @@ int main(void)
// - the tips add up to the same amount // - the tips add up to the same amount
// - try to skip any array (hash map) slots with one or fewer elements stored // - try to skip any array (hash map) slots with one or fewer elements stored
int num_snowflakes = get_num_snowflakes_from_stdin(); 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); populate_snowflakes_from_stdin(snowflakes, num_snowflakes);
for (int i = 0; i < num_snowflakes; i++) // for (int i = 0; i < num_snowflakes; i++)
for (int j = i + 1; j < num_snowflakes; j++) // for (int j = i + 1; j < num_snowflakes; j++)
if (they_are_equal(snowflakes[i], snowflakes[j])) // if (they_are_equal(snowflakes[i], snowflakes[j]))
{ // {
printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES); // printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES);
return 0; // return 0;
} // }
printf(STRING_SNOWFLAKES_ARE_UNIQUE); // printf(STRING_SNOWFLAKES_ARE_UNIQUE);
return 0; return 0;
} }

View File

@@ -1,5 +1,7 @@
4 4
6 5 4 3 2 1 6 5 4 3 2 1
4 6 5 1 2 3
1 2 10 2 1 5
15 12 12 12 12 12 15 12 12 12 12 12
4 5 6 1 2 3 4 5 6 1 2 3
1 1 1 1 1 1 1 1 1 1 1 1