feat: Day1_2
This commit is contained in:
parent
04e8802dac
commit
c871af9701
1 changed files with 14 additions and 6 deletions
|
|
@ -17,6 +17,7 @@ int main() {
|
|||
int *first_numbers = malloc(lines * sizeof(int));
|
||||
int *second_numbers = malloc(lines * sizeof(int));
|
||||
int current_position = 0;
|
||||
int count = 0;
|
||||
int sum = 0;
|
||||
|
||||
file = fopen(filename, "r");
|
||||
|
|
@ -49,8 +50,15 @@ int main() {
|
|||
qsort(second_numbers, lines, sizeof(int), compare_ints);
|
||||
|
||||
for (int i = 0; i < lines; i++) {
|
||||
int difference = abs(first_numbers[i] - second_numbers[i]);
|
||||
sum += difference;
|
||||
for (int j = 0; j < lines; j++) {
|
||||
if (first_numbers[i] == second_numbers[j]) {
|
||||
count++;
|
||||
} else if (count != 0) {
|
||||
sum += first_numbers[i] * count;
|
||||
count = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d\n", sum);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue