From db68cca73ea7f6f043785dfae6f4a6f62b964f7f Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 5 Dec 2022 17:37:00 +0200 Subject: [PATCH] create vec.h --- day2.c | 28 ++++++++++++---------------- day3.c | 49 ++++++++++++++++++++----------------------------- day4.c | 35 ++++++++++++++++------------------- vec.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 64 deletions(-) create mode 100644 vec.h diff --git a/day2.c b/day2.c index 708c29a..99e6600 100644 --- a/day2.c +++ b/day2.c @@ -2,36 +2,32 @@ #include #include "aoc.h" +#include "vec.h" typedef struct { char you, opponent; } Round; -typedef struct { - Round *rounds; - size_t count; -} day2_Data; - static void *day2_parse(char **lines, int line_count) { - day2_Data *data = calloc(1, sizeof(day2_Data)); - data->count = line_count; - data->rounds = calloc(line_count, sizeof(Round)); + Vec *vec = vec_malloc(line_count); for (int i = 0; i < line_count; i++) { - data->rounds[i].opponent = lines[i][0]; - data->rounds[i].you = lines[i][2]; + Round *round = malloc(sizeof(Round)); + round->opponent = lines[i][0]; + round->you = lines[i][2]; + vec_push(vec, round); } - return data; + return vec; } static int day2_part1(void *p) { - day2_Data *data = (day2_Data*)p; + Vec *rounds = p; int result = 0; - for (int i = 0; i < data->count; i++) { - Round *round = &data->rounds[i]; + for (int i = 0; i < rounds->count; i++) { + Round *round = rounds->data[i]; char you = round->you; char opponent = round->opponent; @@ -58,10 +54,10 @@ static int day2_part1(void *p) static int day2_part2(void *p) { - day2_Data *data = (day2_Data*)p; + Vec *data = p; int result = 0; for (int i = 0; i < data->count; i++) { - Round *round = &data->rounds[i]; + Round *round = data->data[i]; char you = round->you; char opponent = round->opponent; diff --git a/day3.c b/day3.c index 18b3bc6..4726c35 100644 --- a/day3.c +++ b/day3.c @@ -4,28 +4,15 @@ #include #include "aoc.h" - -typedef struct { - char *contents; - size_t size; -} Backpack; - -typedef struct { - Backpack *backpacks; - size_t count; -} day3_Data; +#include "vec.h" static void *day3_parse(char **lines, int line_count) { - day3_Data *data = calloc(1, sizeof(day3_Data)); - Backpack *backpacks = calloc(line_count, sizeof(Backpack)); - data->backpacks = backpacks; - data->count = line_count; + Vec *vec = vec_malloc(line_count); for (size_t i = 0; i < line_count; i++) { - backpacks[i].size = strlen(lines[i]); - backpacks[i].contents = lines[i]; + vec_push(vec, lines[i]); } - return data; + return vec; } static bool contains(char needle, char* haystack, size_t len) @@ -60,11 +47,12 @@ static int get_priority(char c) static int day3_part1(void *p) { - day3_Data *data = (day3_Data*)p; + Vec *vec = p; int result = 0; - for (size_t i = 0; i < data->count; i++) { - Backpack *b = &data->backpacks[i]; - char common = find_common(b->contents, b->contents+b->size/2, b->size/2); + for (size_t i = 0; i < vec->count; i++) { + char *b = vec->data[i]; + int size = strlen(b); + char common = find_common(b, b + size/2, size/2); if (common) { result += get_priority(common); } else { @@ -76,17 +64,20 @@ static int day3_part1(void *p) static int day3_part2(void *p) { - day3_Data *data = (day3_Data*)p; + Vec *vec = p; int result = 0; - for (size_t i = 0; i < data->count; i+=3) { - Backpack *b1 = &data->backpacks[i+0]; - Backpack *b2 = &data->backpacks[i+1]; - Backpack *b3 = &data->backpacks[i+2]; + for (size_t i = 0; i < vec->count; i+=3) { + char *b1 = vec->data[i+0]; + char *b2 = vec->data[i+1]; + char *b3 = vec->data[i+2]; + int size1 = strlen(b1); + int size2 = strlen(b2); + int size3 = strlen(b3); bool found = false; - for (size_t j = 0; j < b1->size; j++) { - char c = b1->contents[j]; - if (contains(c, b2->contents, b2->size) && contains(c, b3->contents, b3->size)) { + for (size_t j = 0; j < size1; j++) { + char c = b1[j]; + if (contains(c, b2, size2) && contains(c, b3, size3)) { result += get_priority(c); found = true; break; diff --git a/day4.c b/day4.c index 867b04e..eef4bcb 100644 --- a/day4.c +++ b/day4.c @@ -4,6 +4,7 @@ #include #include "aoc.h" +#include "vec.h" typedef struct { int from, to; @@ -14,11 +15,6 @@ typedef struct { Range second; } DoubleRange; -typedef struct { - DoubleRange *ranges; - size_t count; -} day4_Data; - static inline void day4_parse_range(Range *range, char *s) { range->from = atoi(strsep(&s, "-")); @@ -36,23 +32,23 @@ static void day4_parse_line(DoubleRange *double_range, char *line) static void *day4_parse(char **lines, int line_count) { - day4_Data *data = calloc(1, sizeof(day4_Data)); - DoubleRange *ranges = calloc(line_count, sizeof(DoubleRange)); - data->ranges = ranges; - data->count = line_count; + Vec *vec = vec_malloc(line_count); for (size_t i = 0; i < line_count; i++) { - day4_parse_line(&data->ranges[i], lines[i]); + DoubleRange *double_range = malloc(sizeof(DoubleRange)); + vec_push(vec, double_range); + day4_parse_line(double_range, lines[i]); } - return data; + return vec; } static int day4_part1(void *p) { - day4_Data *data = (day4_Data*)p; + Vec *vec = p; int result = 0; - for (int i = 0; i < data->count; i++) { - Range *range1 = &data->ranges[i].first; - Range *range2 = &data->ranges[i].second; + for (int i = 0; i < vec->count; i++) { + DoubleRange *double_range = vec->data[i]; + Range *range1 = &double_range->first; + Range *range2 = &double_range->second; if ((range1->from <= range2->from && range1->to >= range2->to) || (range2->from <= range1->from && range2->to >= range1->to)) { result++; @@ -63,11 +59,12 @@ static int day4_part1(void *p) static int day4_part2(void *p) { - day4_Data *data = (day4_Data*)p; + Vec *vec = p; int result = 0; - for (int i = 0; i < data->count; i++) { - Range *range1 = &data->ranges[i].first; - Range *range2 = &data->ranges[i].second; + for (int i = 0; i < vec->count; i++) { + DoubleRange *double_range = vec->data[i]; + Range *range1 = &double_range->first; + Range *range2 = &double_range->second; if ((range1->from <= range2->from && range1->to >= range2->to) || (range2->from <= range1->from && range2->to >= range1->to) || MIN(range1->to, range2->to) >= MAX(range1->from, range2->from)) { diff --git a/vec.h b/vec.h new file mode 100644 index 0000000..998e5b9 --- /dev/null +++ b/vec.h @@ -0,0 +1,43 @@ +#ifndef VEC_H_ +#define VEC_H_ + +#include +#include + +typedef struct { + int count; + int capacity; + void **data; +} Vec; + +static inline Vec *vec_malloc(size_t capacity) +{ + Vec *vec = (Vec*)malloc(sizeof(Vec)); + vec->count = 0; + vec->capacity = capacity; + vec->data = (void**)malloc(capacity * sizeof(void*)); + return vec; +} + +static inline void vec_push(Vec *vec, void* value) +{ + if (vec->count >= vec->capacity) { + vec->capacity = (vec->capacity + 1) * 2; + vec->data = (void**)realloc(vec->data, vec->capacity); + } + vec->data[vec->count++] = value; +} + +inline void vec_pop(Vec* vec) +{ + vec->count--; +} + +inline void vec_free(Vec *v) +{ + free(v->data); + free(v); +} + + +#endif //VEC_H_