51 lines
978 B
C
51 lines
978 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "../../lib/aoc.h"
|
|
|
|
int main() {
|
|
char *line;
|
|
int line_count = aoc_line_count();
|
|
|
|
int left[line_count];
|
|
int right[line_count];
|
|
int index = 0;
|
|
|
|
while ((line = aoc_read_line()) != NULL) {
|
|
int l, r;
|
|
sscanf(line, "%d %d", &l, &r);
|
|
left[index] = l;
|
|
right[index] = r;
|
|
index++;
|
|
}
|
|
|
|
qsort(left, line_count, sizeof(int), aoc_sort_int);
|
|
qsort(right, line_count, sizeof(int), aoc_sort_int);
|
|
|
|
int sum = 0;
|
|
|
|
for (int i = 0; i < line_count; i++) {
|
|
sum += abs(left[i] - right[i]);
|
|
}
|
|
|
|
printf("Part 1: %d\n", sum);
|
|
|
|
int similarity_score = 0;
|
|
int j = 0;
|
|
|
|
for (int i = 0; i < line_count; i++) {
|
|
int right_count = 0;
|
|
for (; j < line_count; j++) {
|
|
if (left[i] == right[j]) right_count++;
|
|
else if (left[i] < right[j]) break;
|
|
}
|
|
similarity_score += (left[i] * right_count);
|
|
}
|
|
|
|
printf("Part 2: %d\n", similarity_score);
|
|
|
|
aoc_free();
|
|
return 0;
|
|
}
|