commit 734396f1e1a6ae6e7c9560fa3232e7baf3601f49
parent ead3cc26dcfa67a82e66d190a0696838efcaa063
Author: bsandro <brian.drosan@gmail.com>
Date: Sun, 19 Dec 2021 18:12:07 +0200
Day 10 qs -> stdlib qsort
Diffstat:
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/day07/puzzle.c b/day07/puzzle.c
@@ -15,7 +15,10 @@
#define STR_LEN 16384
static int compare(const void *l, const void *r) {
- return *(long long *)l - *(long long *)r;
+ long long d = *(long long *)l - *(long long *)r;
+ if (d > 0) return 1;
+ if (d < 0) return -1;
+ return 0;
}
void puzzle(const char *filename, size_t *result1, size_t *result2) {
diff --git a/day10/puzzle.c b/day10/puzzle.c
@@ -8,7 +8,6 @@
#include <assert.h>
#include "util.h"
-#include "quicksort.h"
#define STR_LEN 1024
@@ -21,6 +20,13 @@ long long calc_score1(const char illegal);
long long calc_score2(struct array_t *brackets);
long long get_score2(const char c);
+static int compare(const void *l, const void *r) {
+ long long d = *(long long *)l - *(long long *)r;
+ if (d > 0) return 1;
+ if (d < 0) return -1;
+ return 0;
+}
+
void puzzle(const char *filename, long long *result1, long long *result2) {
FILE *infile = fopen(filename, "r");
if (infile == NULL) {
@@ -34,7 +40,6 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
array_init(&scores, sizeof(long long), 10);
*result1 = 0;
- *result2 = 0;
while (fgets(buf, STR_LEN, infile) != NULL) {
struct array_t brackets = { .data = NULL };
@@ -53,8 +58,8 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
bzero(buf, STR_LEN);
}
- long long *scores_data = (long long *)scores.data;
- qs(scores_data, 0, scores.count - 1);
+ long long *scores_data = scores.data;
+ qsort(scores_data, scores.count, scores.elem_size, compare);
*result2 = scores_data[scores.count / 2];
free(scores.data);