feat(01): Complete day 1

Some things should be done in a better way but it's working and it's late.
This commit is contained in:
Jiří Štefka 2024-12-03 04:08:49 +01:00
parent 807fda4356
commit 0ed1982329
Signed by: jiriks74
GPG Key ID: 1D5E30D3DB2264DE
4 changed files with 139 additions and 2 deletions

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

@ -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:
<span title="We were THIS close to summoning the Alot of Location IDs!">a lot</span>
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?_

@ -6,7 +6,30 @@
* @author jiriks74
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#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;
}

6
01/test.input Normal file

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3