commit 19741b87d6bb1ad74418f6f2085b0863c8c3103e Author: Zev Averbach Date: Thu Nov 30 09:50:24 2023 +0100 first, with working snowflakes.c but it doesn't pass the time limit, it's brute force. 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 0000000..87fdbb8 Binary files /dev/null and b/food_line differ diff --git a/food_line.c b/food_line.c new file mode 100644 index 0000000..f4b43c0 --- /dev/null +++ b/food_line.c @@ -0,0 +1,30 @@ +#include + +#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 0000000..636d91c Binary files /dev/null and b/snowflakes/snowflakes differ diff --git a/snowflakes/snowflakes.c b/snowflakes/snowflakes.c new file mode 100644 index 0000000..61cfea5 --- /dev/null +++ b/snowflakes/snowflakes.c @@ -0,0 +1,87 @@ +#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 + +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 +