aoc_omni/c/2024/1/historian_hysteria.c

51 lines
978 B
C
Raw Normal View History

2024-12-01 00:13:20 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../lib/aoc.h"
int main() {
char *line;
2024-12-01 00:25:47 -05:00
int line_count = aoc_line_count();
2024-12-01 00:13:20 -05:00
2024-12-01 00:25:47 -05:00
int left[line_count];
int right[line_count];
2024-12-01 00:13:20 -05:00
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++;
}
2024-12-01 00:25:47 -05:00
qsort(left, line_count, sizeof(int), aoc_sort_int);
qsort(right, line_count, sizeof(int), aoc_sort_int);
2024-12-01 00:13:20 -05:00
int sum = 0;
2024-12-01 00:25:47 -05:00
for (int i = 0; i < line_count; i++) {
2024-12-01 00:13:20 -05:00
sum += abs(left[i] - right[i]);
}
printf("Part 1: %d\n", sum);
int similarity_score = 0;
2024-12-01 00:20:36 -05:00
int j = 0;
2024-12-01 00:25:47 -05:00
for (int i = 0; i < line_count; i++) {
2024-12-01 00:13:20 -05:00
int right_count = 0;
2024-12-01 00:25:47 -05:00
for (; j < line_count; j++) {
2024-12-01 00:13:20 -05:00
if (left[i] == right[j]) right_count++;
2024-12-01 00:20:36 -05:00
else if (left[i] < right[j]) break;
2024-12-01 00:13:20 -05:00
}
similarity_score += (left[i] * right_count);
}
printf("Part 2: %d\n", similarity_score);
aoc_free();
return 0;
}