diff --git a/01/CMakeLists.txt b/01/CMakeLists.txt index 9d75eb2..da4a6b3 100644 --- a/01/CMakeLists.txt +++ b/01/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.14) # Generate compile_commands.json -set(PROJECT_NAME CTemplate) +set(PROJECT_NAME 01) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Turn on testing by default diff --git a/01/README.md b/01/README.md index 569ff04..ae8140c 100644 --- a/01/README.md +++ b/01/README.md @@ -86,3 +86,53 @@ In the example above, this is `2 + 1 + 0 + 1 + 2 + 5`, a total distance of _`11` Your actual left and right lists contain many location IDs. _What is the total distance between your lists?_ + +### Part Two + +Your analysis only confirmed what everyone feared: +the two lists of location IDs are indeed very different. + +Or are they? + +The Historians can't agree on which group made the mistakes _or_ +how to read most of the Chief's handwriting, but in the commotion you notice +an interesting detail: +a lot +of location IDs appear in both lists! +Maybe the other numbers aren't location IDs at all but rather misinterpreted handwriting. + +This time, you'll need to figure out exactly how often each number from the left +list appears in the right list. Calculate a total _similarity score_ +by adding up each number in the left list after multiplying it by the number +of times that number appears in the right list. + +Here are the same example lists again: + +``` +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 +``` + +For these example lists, here is the process of finding the similarity score: + +- The first number in the left list is `3`. +It appears in the right list three times, so the similarity score increases by `3 * 3 = 9`. +- The second number in the left list is `4`. +It appears in the right list once, so the similarity score increases by `4 * 1 = 4`. +- The third number in the left list is `2`. +It does not appear in the right list, so the similarity score does not increase (`2 * 0 = 0`). +- The fourth number, `1`, +also does not appear in the right list. +- The fifth number, `3`, +appears in the right list three times; the similarity score increases by _`9`_. +- The last number, `3`, +appears in the right list three times; the similarity score again increases by _`9`_. + +So, for these example lists, the similarity score at the end of this process +is _`31`_ (`9 + 4 + 0 + 0 + 9 + 9`). + +Once again consider your left and right lists. _What is their similarity score?_ diff --git a/01/src/main.c b/01/src/main.c index 70ba1e7..165cafd 100644 --- a/01/src/main.c +++ b/01/src/main.c @@ -6,7 +6,30 @@ * @author jiriks74 */ +#define _GNU_SOURCE + #include +#include + +#define INC_CHUNK = 64 + +int compareInt(const void *a, const void *b) { + int int_a = *((int *)a); + int int_b = *((int *)b); + return int_a < int_b ? -1 : int_a == int_b ? 0 : 1; +} + +int compare(const void *a, const void *b) { + int int_a = *((int *)a); + int int_b = *((int *)b); + + if (int_a == int_b) + return 0; + else if (int_a < int_b) + return -1; + else + return 1; +} /** * @brief Main entry point @@ -20,6 +43,64 @@ int main(int argc, char *argv[]) int main_test(int argc, char *argv[]) #endif { - printf("Hello world!\n"); + + FILE *file; + if (argc == 1) { + file = stdin; + } else { + file = fopen(argv[1], "r"); + } + + if (file == NULL) { + if (argc > 1) + fprintf(stderr, "Could not open file '%s'\n", argv[1]); + else + fprintf(stderr, "Couldn't open file\n"); + exit(EXIT_FAILURE); + } + + char *buffer = NULL; + size_t bufferSize = 0; + uint lines = 0; + while (getline(&buffer, &bufferSize, file) != -1) { + lines++; + } + + int array1[lines]; + int array2[lines]; + + rewind(file); + + for (uint i = 0; i < lines; i++) { + char *buffer = NULL; + size_t bufferSize = 0; + if (getline(&buffer, &bufferSize, file) == -1) + break; + + sscanf(buffer, "%d %d", &array1[i], &array2[i]); + } + + fclose(file); + + qsort(array1, lines, sizeof(int), compareInt); + qsort(array2, lines, sizeof(int), compareInt); + + uint distanceSum = 0; + for(uint i = 0; i < lines; i++){ + distanceSum += array1[i] < array2[i] ? array2[i] - array1[i] : array1[i] - array2[i]; + } + + uint reoccuranceSum = 0; + for(uint i = 0; i < lines; i++){ + uint reoccurance = 0; + for(uint j = 0; j < lines; j++){ + if(array1[i] == array2[j]) reoccurance++; + } + reoccuranceSum += array1[i] * reoccurance; + } + + printf("The distance between my lists is %d\n", distanceSum); + printf("The simmilarity score is %d\n", reoccuranceSum); + return 0; } diff --git a/01/test.input b/01/test.input new file mode 100644 index 0000000..b8af9ad --- /dev/null +++ b/01/test.input @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3