#include #include #define LINES 1000 int compare(const void *a, const void *b); int main() { // populate arrays from file FILE *input; input = fopen("./input", "r"); unsigned int left[LINES]; unsigned int right[LINES]; for (int i = 0; i < LINES; i++) { fscanf(input, "%d", &left[i]); // printf("%d left: %d ", i, left[i]); fscanf(input, "%d", &right[i]); // printf("%d right: %d\n", i, right[i]); } // sort arrays qsort(left, LINES, sizeof(int), compare); qsort(right, LINES, sizeof(int), compare); /* for (int i = 0; i < LINES; i++) { printf("%d left: %d ", i, left[i]); printf("%d right: %d\n", i, right[i]); } */ // compare each element n, add its difference to sum-var unsigned long int distance = 0; for (int i = 0; i < LINES; i++) { distance += abs(left[i] - right[i]); } printf("distance: %d\n", distance); } 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; }