AoC2024/1/mainB.c
2024-12-03 13:13:13 +01:00

51 lines
1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#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]);
}
/*
for (int i = 0; i < LINES; i++) {
printf("%d left: %d ", i, left[i]);
printf("%d right: %d\n", i, right[i]);
}
*/
// check how many times left[i] is in right[]. multiply left[i] by this.
// then sum it
unsigned long int sum = 0;
for (int i = 0; i < LINES; i++) {
int numRight = 0;
for (int j = 0; j < LINES; j++) {
numRight += left[i] == right[j];
}
sum += left[i] * numRight;
}
printf("sum: %d\n", sum);
}
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;
}