2024-12-07 13:56:24 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 1024
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
FILE *fp = fopen("input", "r");
|
|
|
|
|
|
|
|
size_t lineCount = 0;
|
|
|
|
|
|
|
|
char buffer[BUFFER_SIZE] = {0};
|
|
|
|
|
|
|
|
// get number of lines
|
|
|
|
size_t read = 0;
|
|
|
|
|
|
|
|
while ((read = fread(&buffer, sizeof(char), BUFFER_SIZE, fp))) {
|
|
|
|
for (size_t i = 0; i < read; i++) {
|
|
|
|
if (buffer[i] == '\n') {
|
|
|
|
lineCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
// get data from file
|
2024-12-07 16:27:55 -07:00
|
|
|
int* left = malloc((lineCount * 2 + 1) * sizeof(int));
|
2024-12-07 13:56:24 -07:00
|
|
|
// haha pointer arithmetic go brrr
|
2024-12-07 16:27:55 -07:00
|
|
|
int* right = left + lineCount;
|
2024-12-07 13:56:24 -07:00
|
|
|
|
|
|
|
for (size_t i = 0; i < lineCount; i++) {
|
|
|
|
int leftNum, rightNum;
|
|
|
|
int numsGot = fscanf(fp, "%d %d\n", &leftNum, &rightNum);
|
|
|
|
|
|
|
|
if (numsGot == 2) {
|
|
|
|
left[i] = leftNum;
|
|
|
|
right[i] = rightNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < lineCount - 1; i++) {
|
2024-12-07 17:18:13 -07:00
|
|
|
size_t leftMin = i;
|
|
|
|
size_t rightMin = i;
|
2024-12-07 13:56:24 -07:00
|
|
|
|
|
|
|
for (size_t j = i + 1; j < lineCount; j++) {
|
|
|
|
if (left[j] < left[leftMin]) {
|
|
|
|
leftMin = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (right[j] < right[rightMin]) {
|
|
|
|
rightMin = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leftMin != i) {
|
|
|
|
int temp = left[i];
|
|
|
|
left[i] = left[leftMin];
|
|
|
|
left[leftMin] = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rightMin != i) {
|
|
|
|
int temp = right[i];
|
|
|
|
right[i] = right[rightMin];
|
|
|
|
right[rightMin] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int sum = 0;
|
|
|
|
for (size_t i = 0; i < lineCount; i++) {
|
|
|
|
sum += abs(left[i] - right[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%d\n", sum);
|
|
|
|
|
|
|
|
free(left);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
2024-12-08 01:59:58 -07:00
|
|
|
}
|