This commit is contained in:
Evan Burkey 2024-12-02 07:17:41 -08:00
parent 924caa936c
commit b5f4daede9
3 changed files with 56 additions and 5 deletions

View File

@ -9,7 +9,7 @@
#define MIN(x, y) (x) < (y) ? (x) : (y) #define MIN(x, y) (x) < (y) ? (x) : (y)
char *md5_str(const char *); char *md5_str(const char *);
Vector *string_to_int_vector(const char *input_string, const char *delim); Vector *string_to_int_vector(const char *input_string, const char *delim);
int int_comp(const void *a, const void *b);
#endif #endif

View File

@ -1,10 +1,58 @@
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "lfinput.h" #include "lfinput.h"
#include "advent_utility.h"
void advent2024day01(void) { void advent2024day01(void) {
char *input = get_input("input/2024/01"); size_t sz = 0;
printf("Solution for Day 01 of 2024 is not completed yet\n"); char **input = get_lines("input/2024/01", &sz);
free(input); int a[sz];
int b[sz];
const char* errstr;
for (size_t i = 0; i < sz; i++) {
size_t sp_sz = 0;
char **sp = split(input[i], &sp_sz, " ");
a[i] = (int)strtonum(sp[0], INT_MIN, INT_MAX, &errstr);
if (errstr != NULL) {
printf("ERR: %s\n", errstr);
free(sp);
break;
}
b[i] = (int)strtonum(sp[1], INT_MIN, INT_MAX, &errstr);
if (errstr != NULL) {
printf("ERR: %s\n", errstr);
free(sp);
break;
}
free(sp);
}
qsort(a, sz, sizeof(int), int_comp);
qsort(b, sz, sizeof(int), int_comp);
int p1 = 0;
for (size_t i = 0; i < sz; i++) {
p1 += abs(a[i] - b[i]);
}
printf("%d\n", p1);
int p2 = 0;
for (size_t i = 0; i < sz; i++) {
int c = 0;
for (size_t j = 0; j < sz; j++) {
if (a[i] == b[j]) {
++c;
}
}
p2 += a[i] * c;
}
printf("%d\n", p2);
del_lines(input);
} }

View File

@ -51,3 +51,6 @@ Vector *string_to_int_vector(const char *input_string, const char* delim) {
return v; return v;
} }
int int_comp(const void *a, const void *b) {
return *(int *) a - *(int *) b;
}