advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs

commit 542b61b74650481fe6ba9e123a648cc660b922a9
parent 2a6c6f6869f2fecc7e160716e9bb5be8057c72ba
Author: bsandro <brian.drosan@gmail.com>
Date:   Fri, 10 Dec 2021 22:29:00 +0200

Quicksort works with long long type only now.

Diffstat:
Mcommon/util.c | 21+++++++++++++++++++++
Mcommon/util.h | 1+
Mday07/puzzle.c | 16++++++++--------
Mday07/puzzle_brute.c | 16++++++++--------
4 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/common/util.c b/common/util.c @@ -39,3 +39,24 @@ void parse_numbers_array(struct array_t *numbers, const char *str, const char *d free(tmp); } + +//@todo copypasta +void parse_numbers_array_ll(struct array_t *numbers, const char *str, const char *delim) { + assert(numbers != NULL); + char *tmp = strndup(str, STR_LEN_LIMIT); + char *token = NULL; + assert(tmp != NULL); + + while ((token = strsep(&tmp, delim)) != NULL) { + long long num = atoll(token); + + if (numbers->count >= numbers->cap) { + array_expand(numbers); + } + + long long *data = (long long *)numbers->data; + data[numbers->count++] = num; + } + + free(tmp); +} diff --git a/common/util.h b/common/util.h @@ -35,3 +35,4 @@ void array_expand(struct array_t *array); /* ******************* parse plain numbers array *******************/ void parse_numbers_array(struct array_t *numbers, const char *str, const char *delim); +void parse_numbers_array_ll(struct array_t *numbers, const char *str, const char *delim); diff --git a/day07/puzzle.c b/day07/puzzle.c @@ -26,22 +26,22 @@ void puzzle(const char *filename, size_t *result1, size_t *result2) { unsigned int line_num = 0; struct array_t numbers = { .data = NULL }; - array_init(&numbers, sizeof(int), 100); + array_init(&numbers, sizeof(long long), 100); while (fgets(buf, STR_LEN, infile) != NULL) { - parse_numbers_array(&numbers, buf, ","); + parse_numbers_array_ll(&numbers, buf, ","); ++line_num; bzero(buf, STR_LEN); } - qs((int *)numbers.data, 0, numbers.count - 1); + qs((long long *)numbers.data, 0, numbers.count - 1); // median average size_t index = numbers.count / 2; - int *data = (int *)numbers.data; - int mid = data[index]; + long long *data = (long long *)numbers.data; + long long mid = data[index]; for (size_t i = 0; i < numbers.count; ++i) { - *result1 += abs(data[i] - mid); + *result1 += llabs(data[i] - mid); } // mean average @@ -49,9 +49,9 @@ void puzzle(const char *filename, size_t *result1, size_t *result2) { for (size_t i = 0; i < numbers.count; ++i) { sum += data[i]; } - int avg = floor((sum + 1) / (double)numbers.count); + long long avg = floor((sum + 1) / (double)numbers.count); for (size_t i = 0; i < numbers.count; ++i) { - int dist = abs(data[i] - avg); + long long dist = llabs(data[i] - avg); *result2 += dist * (1 + dist) / 2; } diff --git a/day07/puzzle_brute.c b/day07/puzzle_brute.c @@ -27,30 +27,30 @@ void puzzle_brute(const char *filename, size_t *result1, size_t *result2) { unsigned int line_num = 0; struct array_t numbers = { .data = NULL }; - array_init(&numbers, sizeof(int), 100); + array_init(&numbers, sizeof(long long), 100); while (fgets(buf, STR_LEN, infile) != NULL) { - parse_numbers_array(&numbers, buf, ","); + parse_numbers_array_ll(&numbers, buf, ","); ++line_num; bzero(buf, STR_LEN); } - qs((int *)numbers.data, 0, numbers.count - 1); + qs((long long *)numbers.data, 0, numbers.count - 1); // median average size_t index = numbers.count / 2; - int *data = (int *)numbers.data; - int mid = data[index]; + long long *data = (long long *)numbers.data; + long long mid = data[index]; for (size_t i = 0; i < numbers.count; ++i) { - *result1 += abs(data[i] - mid); + *result1 += llabs(data[i] - mid); } // dumb bruteforcing *result2 = ULONG_MAX; - for (int i = data[0]; i < data[numbers.count - 1]; ++i) { + for (long long i = data[0]; i < data[numbers.count - 1]; ++i) { size_t sum = 0; for (size_t j = 0; j < numbers.count; ++j) { - int dist = abs(i - data[j]); + long long dist = llabs(i - data[j]); sum += dist * (1 + dist) / 2; } *result2 = fmin(*result2, (double)sum);