From 19741b87d6bb1ad74418f6f2085b0863c8c3103e Mon Sep 17 00:00:00 2001 From: Zev Averbach Date: Thu, 30 Nov 2023 09:50:24 +0100 Subject: [PATCH] first, with working snowflakes.c but it doesn't pass the time limit, it's brute force. --- README.md | 1 + food_line | Bin 0 -> 33248 bytes food_line.c | 30 ++++++ snowflakes/Makefile | 6 ++ ...understand_snowflakes_optimized_solution.c | 33 +++++++ ...tand_snowflakes_optimized_solution_WRONG.c | 28 ++++++ ...and_snowflakes_optimized_solution_WRONG2.c | 29 ++++++ ...and_snowflakes_optimized_solution_WRONG3.c | 31 +++++++ snowflakes/snowflakes | Bin 0 -> 33584 bytes snowflakes/snowflakes.c | 87 ++++++++++++++++++ snowflakes/snowflakes.md | 25 +++++ snowflakes/snowflakes.txt | 6 ++ 12 files changed, 276 insertions(+) create mode 100644 README.md create mode 100755 food_line create mode 100644 food_line.c create mode 100644 snowflakes/Makefile create mode 100644 snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c create mode 100644 snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c create mode 100644 snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c create mode 100644 snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c create mode 100755 snowflakes/snowflakes create mode 100644 snowflakes/snowflakes.c create mode 100644 snowflakes/snowflakes.md create mode 100644 snowflakes/snowflakes.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad16203 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +This repo is to document my progress working through the book "Algorithmic Thinking". diff --git a/food_line b/food_line new file mode 100755 index 0000000000000000000000000000000000000000..87fdbb89434ca5c9eda3bc6c68f9275de55a1006 GIT binary patch literal 33248 zcmeI5&ud&&6vyvtT3gfF7m8Fx!P!KrLPBlD+EARddH9NH8%!!e+uly*M`mm?3G)M! zh%zA^$Ye50q2R_pAqYj>XiAGyLC~eU&O#x95eh9BU3C1O``$}uUUVb4k?$er-E+>p z_q=mIbF;kf{`lu_kM=5Mbt$EGL3>1ZD^*qzSV}zyb)j0148HBW@4S0KqMc%Ox_|3Q zo@ETwddwLg>l|X{M>}K6_xB(yijs_IT}aLqd)}(|*7B?c$fc9n!j( z{V0=7=u9>>gJ`(EFZ_Jt{tWTMIkuC*kl*13RO_L^u|a+2!ue6}*NA%S47i6|*&m|9 zR`!V2lQV5U!$0PWV62g-H{bV^@REFiJl<{=N4HNYGeAC2_ zV}{fx^JJaJeE$Y~*~h}>S?C6Q?0Yr0s8ky5e&`6aU4k`t?4R}lu*Wf9@`U9e0ukr| zXn!%E>z~R@^e5)067WTyhe{nke><8ycJHLS@?rYRKTclxy8{-zLBy?nHjD zYmps271QI*ubB65VvL{AlqYF6KSxp3oAP2T<5LX4GCuHiZz%X=O3 zL3vH4fO7TRmiy*iyDVpmkFL9mcjQ;Ne0xLA4bUb}cr(&6@&d6jj zmP;r-o6BSiDW&uASTEd2NhfJ+OKvvz>O5?G@7{D-uY6mw>hMg + +#define MAX_LINES 100 + +int main() +{ + int i, j, num_lines, num_new_people; + int lines[MAX_LINES]; + scanf("%d%d", &num_lines, &num_new_people); + for (i = 0; i < num_lines; i++) + { + scanf("%d", &lines[i]); + } + int smallest; + int smallest_index = -1; + for (i = 0; i < num_new_people; i++) + { + smallest = 101; + for (j = 0; j < num_lines; j++) + { + if (lines[j] < smallest) { + smallest = lines[j]; + smallest_index = j; + } + } + printf("%d\n", smallest); + lines[smallest_index] += 1; + } + return 0; +} diff --git a/snowflakes/Makefile b/snowflakes/Makefile new file mode 100644 index 0000000..3c2f31b --- /dev/null +++ b/snowflakes/Makefile @@ -0,0 +1,6 @@ +build: + clang snowflakes.c -o snowflakes +run: + ./snowflakes < snowflakes.txt +go: + clang snowflakes.c -o snowflakes && ./snowflakes < snowflakes.txt diff --git a/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c new file mode 100644 index 0000000..427899f --- /dev/null +++ b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c @@ -0,0 +1,33 @@ +#include + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +int main() +{ + Node nodes[5]; + for (int i = 0; i < 5; i++) + { + nodes[i].value = i + 42; + if (i > 0) + { + nodes[i - 1].next = &nodes[i]; + } + } + for (int i = 0; i < 5; i++) + { + printf("node at %d has value %d\n", i, nodes[i].value); + if (i > 0) + { + printf("node at %d has value %d\n", i - 1, nodes[i - 1].value); + printf( + "that node's 'next' is %p and, as expected, its 'next' has value %d\n", + nodes[i - 1].next, nodes[i - 1].next->value + ); + } + } + return 0; +} diff --git a/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c new file mode 100644 index 0000000..583ad63 --- /dev/null +++ b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c @@ -0,0 +1,28 @@ +#include +#include + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +int main(void) +{ + Node nodes[5]; + + for (int i = 0; i < 5; i++) + { + Node curr_node = { i }; + nodes[i] = curr_node; + printf("Node in ARR current after insertion: %d\n", nodes[i].value); + printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]); + } + + for (int i = 0; i < 5; i++) + { + printf("Node in ARR: %d\n", nodes[i].value); + } + + return 0; +} diff --git a/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c new file mode 100644 index 0000000..fe7e857 --- /dev/null +++ b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c @@ -0,0 +1,29 @@ +#include +#include + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +int main(void) +{ + Node *nodes[5]; + + for (int i = 0; i < 5; i++) + { + Node * curr_node = malloc(sizeof(Node)); + curr_node->value = i; + nodes[i] = curr_node; + printf("Node in ARR current after insertion: %d\n", nodes[i]->value); + printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]); + } + + for (int i = 0; i < 5; i++) + { + printf("Node in ARR: %d\n", nodes[i]->value); + } + + return 0; +} diff --git a/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c new file mode 100644 index 0000000..78f1912 --- /dev/null +++ b/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c @@ -0,0 +1,31 @@ +#include +#include + +#define ARRAY_LEN 1047505 + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +Node * make_node(int value) +{ + Node * node = malloc(sizeof(Node)); + node->value = value; + return node; +} + +int main() +{ + Node * nodes[ARRAY_LEN]; + for (int i = 0; i < ARRAY_LEN; i++) + { + nodes[i] = make_node(i); + } + for (int i = 0; i < ARRAY_LEN; i++) + { + printf("Node in ARR: %d\n", nodes[i]->value); + } + return 0; +} diff --git a/snowflakes/snowflakes b/snowflakes/snowflakes new file mode 100755 index 0000000000000000000000000000000000000000..636d91c7048d997cfd5a30dc9a82ec3029fb6d7e GIT binary patch literal 33584 zcmeI5U1(fI6vyY9^!sCjplz|}7TVG(q(QB;#OSv6>aChK*aS*T9k=_DEZOYF-Mgfz z23EI)UM~w3q<(-<@uetYo|K9YD=poENc#|BAMy}o#a&oo9|Db1yZ+DIyUpe{Rtoy$ z88|a@&di)S^P9PkGxypbzuj6Ugw`g6xDV1n(k{el;YUq~4G;q&W#66`^dtH!2g$lH zX$#dwHS%)?9i{Bo5BD$ZqQ(an+Jvhwftds$sU)QtIbpgA3a`IxHx>i?HChl7>CLgq z&@Qs7bTcW_X6AVMw#n=7;Iodu^==Q+Thnsa-%{5oWhiZ?Vu``q`oi@Ny8d>%?WDIR z+Jn|4WhOBeON3=C5ly1O8{c`?-(hzU>0TeL{%9XQP=5C9>E9!tJ9yxbvl{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2l z2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1pX@memL%f z3_<8AjMfJN-24BqI{y+^m1aGFheZNSi!+WMkfzsGHnJ!!(^7xU@X4SGMEXb!a|OwVhJ-UWI7Z~MBz)5f*$BlKQ(U?*)jZ;KKmxJ z`blJk6UZjV&>D|KO|dZ*8yqslrZ#bX9OJf%C30j)MovUR88ae>BEfJZB~}bZOqs}x z$oaP!WHgn8ZiW%%nvo##`$vH$~Jy85K+?;LcD zqoFEYK;Ei< +#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 + +int get_num_snowflakes_from_stdin(void) +{ + int num_snowflakes; + scanf("%d", &num_snowflakes); + return num_snowflakes; +} + +bool they_are_equal_right(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset) +{ + int sf1_tip, sf2_tip, sf2_index; + for (int i = 0; i < LEN_SNOWFLAKE; i++) + { + sf1_tip = sf1[i]; + + sf2_index = (i + sf2_offset) % LEN_SNOWFLAKE; + sf2_tip = sf2[sf2_index]; + + if (sf1_tip != sf2_tip) + return false; + } + return true; +} + +bool they_are_equal_left(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset) +{ + int sf1_tip, sf2_tip, sf2_index; + for (int i = 0; i < LEN_SNOWFLAKE; i++) + { + sf1_tip = sf1[i]; + + sf2_index = sf2_offset - i; + if (sf2_index < 0) + sf2_index = sf2_index + LEN_SNOWFLAKE; + sf2_tip = sf2[sf2_index]; + + if (sf1_tip != sf2_tip) + return false; + } + return true; +} + +bool they_are_equal(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE]) +{ + for (int o = 0; o < LEN_SNOWFLAKE; o++) + { + if (they_are_equal_right(sf1, sf2, o)) + return true; + if (they_are_equal_left(sf1, sf2, o)) + return true; + } + return false; +} + +void populate_snowflakes_from_stdin(int snowflakes[][LEN_SNOWFLAKE], int num_snowflakes) +{ + for (int i = 0; i < num_snowflakes; i++) + for (int j = 0; j < LEN_SNOWFLAKE; j++) + scanf("%d", &snowflakes[i][j]); +} + +int main(void) +{ + // TODO: refactor to only compare similar snowflakes: + // - 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]; + 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; + } + + printf(STRING_SNOWFLAKES_ARE_UNIQUE); + return 0; +} diff --git a/snowflakes/snowflakes.md b/snowflakes/snowflakes.md new file mode 100644 index 0000000..11b2f65 --- /dev/null +++ b/snowflakes/snowflakes.md @@ -0,0 +1,25 @@ +Two snowflakes are identical if they are the same, +if we can make them the same by moving rightward +through one of the snowflakes (moving clockwise), +or if we can make them the same by moving leftward +through one of the snowflakes (moving counterclockwise). + +# Input + +The first line of input is an integer n, +the number of snowflakes that we’ll be processing. +The value n will be between 1 and 100,000. + +Each of the following n lines represents one snowflake: + each line has six integers, where each integer is + at least 0 and at most 10,000,000. + +# Output +Our output will be a single line of text: + +If there are no identical snowflakes, output exactly + "No two snowflakes are alike." +If there are at least two identical snowflakes, output exactly + "Twin snowflakes found." + +The time limit for solving the test cases is one second. diff --git a/snowflakes/snowflakes.txt b/snowflakes/snowflakes.txt new file mode 100644 index 0000000..8a7cdaf --- /dev/null +++ b/snowflakes/snowflakes.txt @@ -0,0 +1,6 @@ +4 +6 5 4 3 2 1 +15 12 12 12 12 12 +4 5 6 1 2 3 +1 1 1 1 1 1 +