C 2024 day 1
This commit is contained in:
parent
2665c03833
commit
4998077a48
50
c/2024/1/historian_hysteria.c
Normal file
50
c/2024/1/historian_hysteria.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "../../lib/aoc.h"
|
||||||
|
|
||||||
|
int sort(const void *a, const void *b) {
|
||||||
|
return *(int*) a - *(int*) b;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
int left[1000];
|
||||||
|
int right[1000];
|
||||||
|
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, 1000, sizeof(int), sort);
|
||||||
|
qsort(right, 1000, sizeof(int), sort);
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
sum += abs(left[i] - right[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Part 1: %d\n", sum);
|
||||||
|
|
||||||
|
int similarity_score = 0;
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
int right_count = 0;
|
||||||
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
if (left[i] == right[j]) right_count++;
|
||||||
|
}
|
||||||
|
similarity_score += (left[i] * right_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Part 2: %d\n", similarity_score);
|
||||||
|
|
||||||
|
aoc_free();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user