aoc

Advent of code
git clone https://codeberg.org/night0721/aoc
Log | Files | Refs

commit 08dd50e5d2ab79fe06296074e5cb5514cd1539f4
parent 7b6cb31d1853481c87c9818a899d73d6f28e4b86
Author: night0721 <[email protected]>
Date:   Mon,  4 Dec 2023 23:04:04 +0000

use custom header file to remove duplication

Diffstat:
M2023/1/2.c | 24++----------------------
M2023/2/1.c | 68++------------------------------------------------------------------
M2023/2/2.c | 68++------------------------------------------------------------------
M2023/4/1.c | 68++------------------------------------------------------------------
Mlib/util.c | 22++++++++++++++++++++++
Mlib/util.h | 3+--
6 files changed, 31 insertions(+), 222 deletions(-)

diff --git a/2023/1/2.c b/2023/1/2.c @@ -1,3 +1,5 @@ +#include "../../util.h" + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -7,28 +9,6 @@ typedef struct { char* value; } number; -void replace(char *target, const char *needle, const char *replacement) -{ - char buffer[1024] = { 0 }; - char *insert_point = &buffer[0]; - const char *tmp = target; - size_t needle_len = strlen(needle); - size_t repl_len = strlen(replacement); - while (1) { - const char *p = strstr(tmp, needle); - if (p == NULL) { // walked past last occurrence of needle; copy remaining part - strcpy(insert_point, tmp); - break; - } - memcpy(insert_point, tmp, p - tmp); // copy part before needle - insert_point += p - tmp; - memcpy(insert_point, replacement, repl_len); // copy replacement string - insert_point += repl_len; - tmp = p + needle_len; - } - strcpy(target, buffer); -} - int main() { FILE *inputfile = fopen("./input", "r"); char *line = malloc(sizeof(char) * 100); diff --git a/2023/2/1.c b/2023/2/1.c @@ -1,3 +1,5 @@ +#include "../../util.h" + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -10,72 +12,6 @@ typedef struct { int colors[3]; } game; -char** str_split(char* a_str, const char a_delim) -{ - char** result = 0; - size_t count = 0; - char* tmp = a_str; - char* last_comma = 0; - char delim[2]; - delim[0] = a_delim; - delim[1] = 0; - /* Count how many elements will be extracted. */ - while (*tmp) - { - if (a_delim == *tmp) - { - count++; - last_comma = tmp; - } - tmp++; - } - /* Add space for trailing token. */ - count += last_comma < (a_str + strlen(a_str) - 1); - /* Add space for terminating null string so caller - knows where the list of returned strings ends. */ - count++; - result = malloc(sizeof(char*) * count); - if (result) - { - size_t idx = 0; - char* token = strtok(a_str, delim); - while (token) - { - assert(idx < count); - *(result + idx++) = strdup(token); - token = strtok(0, delim); - } - assert(idx == count - 1); - *(result + idx) = 0; - } - return result; -} - -// Note: This function returns a pointer to a substring of the original string. -// If the given string was allocated dynamically, the caller must not overwrite -// that pointer with the returned value, since the original pointer must be -// deallocated using the same allocator with which it was allocated. The return -// value must NOT be deallocated using free() etc. -char *trimwhitespace(char *str) -{ - char *end; - - // Trim leading space - while(isspace((unsigned char)*str)) str++; - - if(*str == 0) // All spaces? - return str; - - // Trim trailing space - end = str + strlen(str) - 1; - while(end > str && isspace((unsigned char)*end)) end--; - - // Write new null terminator character - end[1] = '\0'; - - return str; -} - int main() { FILE *inputfile = fopen("./input", "r"); char line[1024] = { 0 }; diff --git a/2023/2/2.c b/2023/2/2.c @@ -1,3 +1,5 @@ +#include "../../util.h" + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -9,72 +11,6 @@ typedef struct { int colors[3]; } game; -char** str_split(char* a_str, const char a_delim) -{ - char** result = 0; - size_t count = 0; - char* tmp = a_str; - char* last_comma = 0; - char delim[2]; - delim[0] = a_delim; - delim[1] = 0; - /* Count how many elements will be extracted. */ - while (*tmp) - { - if (a_delim == *tmp) - { - count++; - last_comma = tmp; - } - tmp++; - } - /* Add space for trailing token. */ - count += last_comma < (a_str + strlen(a_str) - 1); - /* Add space for terminating null string so caller - knows where the list of returned strings ends. */ - count++; - result = malloc(sizeof(char*) * count); - if (result) - { - size_t idx = 0; - char* token = strtok(a_str, delim); - while (token) - { - assert(idx < count); - *(result + idx++) = strdup(token); - token = strtok(0, delim); - } - assert(idx == count - 1); - *(result + idx) = 0; - } - return result; -} - -// Note: This function returns a pointer to a substring of the original string. -// If the given string was allocated dynamically, the caller must not overwrite -// that pointer with the returned value, since the original pointer must be -// deallocated using the same allocator with which it was allocated. The return -// value must NOT be deallocated using free() etc. -char *trimwhitespace(char *str) -{ - char *end; - - // Trim leading space - while(isspace((unsigned char)*str)) str++; - - if(*str == 0) // All spaces? - return str; - - // Trim trailing space - end = str + strlen(str) - 1; - while(end > str && isspace((unsigned char)*end)) end--; - - // Write new null terminator character - end[1] = '\0'; - - return str; -} - int main() { FILE *inputfile = fopen("./input", "r"); char line[1024] = { 0 }; diff --git a/2023/4/1.c b/2023/4/1.c @@ -1,3 +1,5 @@ +#include "../../util.h" + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -5,72 +7,6 @@ #include <stdbool.h> #include <assert.h> -char** str_split(char* a_str, const char a_delim) -{ - char** result = 0; - size_t count = 0; - char* tmp = a_str; - char* last_comma = 0; - char delim[2]; - delim[0] = a_delim; - delim[1] = 0; - /* Count how many elements will be extracted. */ - while (*tmp) - { - if (a_delim == *tmp) - { - count++; - last_comma = tmp; - } - tmp++; - } - /* Add space for trailing token. */ - count += last_comma < (a_str + strlen(a_str) - 1); - /* Add space for terminating null string so caller - knows where the list of returned strings ends. */ - count++; - result = malloc(sizeof(char*) * count); - if (result) - { - size_t idx = 0; - char* token = strtok(a_str, delim); - while (token) - { - assert(idx < count); - *(result + idx++) = strdup(token); - token = strtok(0, delim); - } - assert(idx == count - 1); - *(result + idx) = 0; - } - return result; -} - -// Note: This function returns a pointer to a substring of the original string. -// If the given string was allocated dynamically, the caller must not overwrite -// that pointer with the returned value, since the original pointer must be -// deallocated using the same allocator with which it was allocated. The return -// value must NOT be deallocated using free() etc. -char *trimwhitespace(char *str) -{ - char *end; - - // Trim leading space - while(isspace((unsigned char)*str)) str++; - - if(*str == 0) // All spaces? - return str; - - // Trim trailing space - end = str + strlen(str) - 1; - while(end > str && isspace((unsigned char)*end)) end--; - - // Write new null terminator character - end[1] = '\0'; - - return str; -} - int main() { FILE *inputfile = fopen("./input", "r"); char line[2048] = { 0 }; diff --git a/lib/util.c b/lib/util.c @@ -64,3 +64,25 @@ char *trimwhitespace(char *str) end[1] = '\0'; return str; } + +void replace(char *target, const char *needle, const char *replacement) +{ + char buffer[1024] = { 0 }; + char *insert_point = &buffer[0]; + const char *tmp = target; + size_t needle_len = strlen(needle); + size_t repl_len = strlen(replacement); + while (1) { + const char *p = strstr(tmp, needle); + if (p == NULL) { // walked past last occurrence of needle; copy remaining part + strcpy(insert_point, tmp); + break; + } + memcpy(insert_point, tmp, p - tmp); // copy part before needle + insert_point += p - tmp; + memcpy(insert_point, replacement, repl_len); // copy replacement string + insert_point += repl_len; + tmp = p + needle_len; + } + strcpy(target, buffer); +} diff --git a/lib/util.h b/lib/util.h @@ -1,4 +1,3 @@ - char** str_split(char* a_str, const char a_delim); char* trimwhitespace(char *str); - +void replace(char *target, const char *needle, const char *replacement);