advent2021

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

commit 053d6493b1542a2853517af840d48f3fb34cec4b
parent bc0efbf0c64482d673e34dee30e498dbe4dec2a8
Author: bsandro <brian.drosan@gmail.com>
Date:   Sun,  5 Dec 2021 10:14:29 +0200

Moved some common stuff into separate header

Diffstat:
Acommon/util.h | 40++++++++++++++++++++++++++++++++++++++++
Mday04/Makefile | 2+-
Mday04/puzzle12.c | 46+---------------------------------------------
3 files changed, 42 insertions(+), 46 deletions(-)

diff --git a/common/util.h b/common/util.h @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <assert.h> + +#define PRINT_ERR(func) fprintf(stderr, #func "() error: %s\n", strerror(errno)); + +#define FMT_(l) #l +#define FMT(l) FMT_(l) + +struct array_t { + void *data; + size_t elem_size; + size_t count; + size_t cap; +}; + +#define PRINT_ARRAY(array, type) do { \ + printf("array count %zu, cap %zu, elem size %zu, data %p\n", \ + array.count, array.cap, array.elem_size, &array); \ + for (size_t i = 0; i < array.count; ++i) { \ + type *data = (type *)array.data; \ + printf("%d ", data[i]); \ + } \ + printf("\n"); \ +} while (0); + +void array_init(struct array_t *array, size_t elem_size, size_t cap) { + assert(array->data == NULL); + array->cap = cap; + array->elem_size = elem_size; + array->data = calloc(cap, elem_size); + assert(array->data != NULL); +} + +void array_expand(struct array_t *array) { + size_t new_cap = array->cap * 2; + array->data = realloc(array->data, array->elem_size * new_cap); + assert(array->data != NULL); + array->cap = new_cap; +} + diff --git a/day04/Makefile b/day04/Makefile @@ -4,7 +4,7 @@ SRC=$(wildcard *.c) DEPS:=$(wildcard *.h) OBJ:=$(SRC:.c=.o) -CFLAGS=-O2 -std=c99 -Werror -Wall -Wextra -I. +CFLAGS=-O2 -std=c99 -Werror -Wall -Wextra -I. -I../common all: $(NAME) diff --git a/day04/puzzle12.c b/day04/puzzle12.c @@ -9,16 +9,9 @@ #include <assert.h> #include <time.h> -#define PRINT_ERR(func) fprintf(stderr, #func "() error: %s\n", strerror(errno)); - -#define ARRAY_PUSH - -#define FMT_(l) #l -#define FMT(l) FMT_(l) +#include "util.h" #define STR_LEN 16384 -#define FMT_STRING "%" FMT(STR_LEN) "s" - #define BOARD_SIZE 5 struct board_t { @@ -29,26 +22,6 @@ struct board_t { bool won; }; -struct array_t { - void *data; - size_t elem_size; - size_t count; - size_t cap; -}; - -void array_init(struct array_t *array, size_t elem_size, size_t cap); -void array_expand(struct array_t *array); - -#define PRINT_ARRAY(array, type) do { \ - printf("array count %zu, cap %zu, elem size %zu, data %p\n", \ - array.count, array.cap, array.elem_size, &array); \ - for (size_t i = 0; i < array.count; ++i) { \ - type *data = (type *)array.data; \ - printf("%d ", data[i]); \ - } \ - printf("\n"); \ -} while (0); - void make_numbers_array(struct array_t *numbers, const char *str); void fill_board(struct board_t *board, int row, const char *str); int check_board(struct board_t *board, int num); @@ -213,20 +186,3 @@ void fill_board(struct board_t *board, int row, const char *str) { } assert(col == BOARD_SIZE); } - -// @todo move generic service stuff to some kind of shared header? - -void array_init(struct array_t *array, size_t elem_size, size_t cap) { - assert(array->data == NULL); - array->cap = cap; - array->elem_size = elem_size; - array->data = calloc(cap, elem_size); - assert(array->data != NULL); -} - -void array_expand(struct array_t *array) { - size_t new_cap = array->cap * 2; - array->data = realloc(array->data, array->elem_size * new_cap); - assert(array->data != NULL); - array->cap = new_cap; -}