From 1dd443cbba465acdf8fe614d9995ba66c6da1635 Mon Sep 17 00:00:00 2001 From: Zev Averbach Date: Thu, 30 Nov 2023 13:47:21 +0100 Subject: [PATCH] second --- snowflakes/TODO.md | 9 +++++++ snowflakes/snowflakes | Bin 33584 -> 33624 bytes snowflakes/snowflakes.c | 53 ++++++++++++++++++++++++++++---------- snowflakes/snowflakes.txt | 2 ++ 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 snowflakes/TODO.md 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 636d91c7048d997cfd5a30dc9a82ec3029fb6d7e..07761235396dbd0ed5995eeeb471e563bd3520b9 100755 GIT binary patch delta 1095 zcmZ8gT}V@582--A6w{4ODU}Ge0t2awjfj5y*#bZNB-9`T3Dz8II$@gW28yk-A7-A8 zgUNRxr5C&EdNd*O4iLGyV* zllahN=|qS_2o3U};Initf9q}(Y-P5!Od^3=Ia}k3%Brbog@%D$Q`uoPca47JesII| zh34G(b>K80tj=s0AwAfs2tqR029WC@hrc=ib@U`};t0LM+l(8h>HN*5&eg}gw|cjJ z-uO!s{6&q?M&I!ROEb zn)Wz7cLOdTazG19f=h5o&YogZE8PtoyIqA`-7XSoQBCkHghm{2?P6CE}R!jMwf9ZV?3qKBIqje2>DRme&_B==7sP3&hK}=^Zna7OUzkT z=PdJE6;myQaD;>sj}D)x&M&sNpJ=^EnKJ%7jY7 zPF&b#N76Tc>$)UpI0`mKrEgSfPa~m~DtWFnk3=*Y<7qUd(fSQ4GnXIeW)t2iIi#fz zAyZsP=F%ry1dUf@qm-mkNn&*?f3r zYMHy@cmjil@57iC$A!-*iexS_1wq$|T&m2%zd_`(Co_IF2*nY(3rEChhxtDaT1r!C zLl(9r;T1-df(ILs+`dDbH~HCvz%q8EY|na{0lrwTdkU-9Ju~!kL%)Eo>m%2~{(x;X zFckIngGDU$EL?Q@G`Vs9uYx_zRjPaGkI>cUKQLFsLeKR_zf^V2nvV?m!d?nZrX@lMDy zNE)ZY_IYleTZXXIMsA*C7eDt@kHWH>viqPLO-$Ny`{>NrA4cUYLbr*xT61{7#4ZCz z5XdA}agrTagut?;@v*nP7r)$xKkUO__F?wc*>70{VgCiYxR7yqj8y$IC)5WsYZd$F GPsm@@lS-Tb 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