From eba796a33d725f7db320a9a3381c65f18b0bf979 Mon Sep 17 00:00:00 2001 From: Nicholas Hope Date: Fri, 23 Sep 2022 16:11:08 -0400 Subject: [PATCH] Added free() calls --- boilerplate.c | 61 ++++++++++++++++++++++++++------------------------- boilerplate.h | 12 +++++++--- solvedle.c | 44 +++++++++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 40 deletions(-) diff --git a/boilerplate.c b/boilerplate.c index c056a5d..d4a9430 100644 --- a/boilerplate.c +++ b/boilerplate.c @@ -35,36 +35,6 @@ uint32_t rmdups(signature * arr, uint32_t nitems) { return nitems; } -void printDashed(uint32_t n) { - for (uint32_t i = A_BIT; i < Z_BIT; i <<= 1) { - if (i & n) { - char c = 'A'; - uint32_t icopy = A_BIT; - while (icopy < i) { - c++; - icopy <<=1 ; - } - putchar(c); - } else { - putchar('-'); - } - } -} - -void printsigs(signature* sigs, uint32_t nitems, ...) { - va_list args; - va_start(args, nitems); - - for (uint32_t i = 0; i < nitems; i++) { - uint32_t index = va_arg(args, uint32_t); - signature s = sigs[index]; - printDashed(s.sign); - printf(": %s : %04d\n", s.word, index); - } - - va_end(args); -} - signature* makeArray(FILE * f, uint32_t* arrlen) { signature* sigs = (signature*) calloc(WORDS, sizeof(signature)); char word[WORDLEN + sizeof(char)]; // 5 letters + \n @@ -86,4 +56,35 @@ signature* makeArray(FILE * f, uint32_t* arrlen) { *arrlen = i; return sigs; +} + +void printDashed(uint32_t n) { + for (uint32_t i = A_BIT; i < Z_BIT; i <<= 1) { + if (i & n) { + char c = 'A'; + uint32_t bit = A_BIT; + while (bit < i) { + c++; + bit <<= 1; + } + putchar(c); + } else { + putchar('-'); + } + } +} + +void printsigs(signature* sigs, uint32_t nitems, ...) { + va_list args; + va_start(args, nitems); + + for (uint32_t i = 0; i < nitems; i++) { + uint32_t index = va_arg(args, uint32_t); + signature s = sigs[index]; + printDashed(s.sign); + printf(": %s : %-4d\n", s.word, index); + } + putchar('\n'); + + va_end(args); } \ No newline at end of file diff --git a/boilerplate.h b/boilerplate.h index d064c08..0e04c96 100644 --- a/boilerplate.h +++ b/boilerplate.h @@ -40,9 +40,15 @@ uint32_t rawWC = 0; #if PRINT_INTERMEDIATES - #define PRINT2 printsigs(sigs, 2, i1, i2); - #define PRINT3 printsigs(sigs, 3, i1, i2, i3); - #define PRINT4 printsigs(sigs, 4, i1, i2, i3, i4); + #if FANCYPRINT + #define PRINT2 printsigs(sigs, 2, i1, i2); + #define PRINT3 printsigs(sigs, 3, i1, i2, i3); + #define PRINT4 printsigs(sigs, 4, i1, i2, i3, i4); + #else + #define PRINT2 printf("%s, %s\n", sigs[i1], sigs[i2]); + #define PRINT3 printf("%s, %s, %s\n", sigs[i1], sigs[i2], sigs[i3]); + #define PRINT4 printf("%s, %s, %s, %s\n", sigs[i1], sigs[i2], sigs[i3], sigs[i4]); + #endif #else #define PRINT2 0; #define PRINT3 0; diff --git a/solvedle.c b/solvedle.c index 73f65e9..5863799 100644 --- a/solvedle.c +++ b/solvedle.c @@ -55,45 +55,71 @@ int main(int argc, char * argv[]) { #if PRINT_INTERMEDIATES fprintf(stderr, "INTERMEDIATE: ended with %u totally unique words\n", cookedWC); #endif - START_TIME("finding words") findWords(sigs, cookedWC); - END_TIME #if TIME_SECTIONS clock_t prog_end = clock(); fprintf(stderr, "%s took %fs total\n", argv[0], ((double)(prog_end - prog_start)) / CLOCKS_PER_SEC); #endif + #if FREE + for (uint32_t i = 0; i < fiveUniqWC; i++) { + free(sigs[i].word); + } + free(sigs); + #endif + return 0; } void findWords(signature* sigs, uint32_t len) { + START_TIME("finding starts") + // for each index, locate the first index of + // a word that shares no letters with it, and + // store the distance between the two. + uint32_t * starts = calloc(len, sizeof(uint32_t)); + for (uint32_t i = 0, j = 0; i < len && j < len; i++) { + uint32_t sig = sigs[i].sign; + for (j = i+1; j < len; j++) { + if ((sig & sigs[j].sign) == 0) { + starts[i] = j - i; + break; + } + } + } + END_TIME + START_TIME("finding words") + // the big loop. for (uint32_t i1 = 0; i1 < len; i1++) { uint32_t a = sigs[i1].sign; - for (uint32_t i2 = i1+1; i2 < len; i2++) { + for (uint32_t i2 = starts[i1]+i1; i2 < len; i2++) { uint32_t b = sigs[i2].sign; if (a & b) { continue; } uint32_t ab = a | b; PRINT2 - for (uint32_t i3 = i2+1; i3 < len; i3++) { + for (uint32_t i3 = starts[i2]+i2; i3 < len; i3++) { uint32_t c = sigs[i3].sign; if (ab & c) { continue; } uint32_t abc = ab | c; PRINT3 - for (uint32_t i4 = i3+1; i4 < len; i4++) { + for (uint32_t i4 = starts[i3]+i3; i4 < len; i4++) { uint32_t d = sigs[i4].sign; if (abc & d) { continue; } uint32_t abcd = abc | d; PRINT4 - for (uint32_t i5 = i4+1; i5 < len; i5++) { + for (uint32_t i5 = starts[i4]+i4; i5 < len; i5++) { uint32_t e = sigs[i5].sign; if (abcd & e) { continue; } - printsigs(sigs, 5, i1, i2, i3, i4, i5); + #if FANCYPRINT + printsigs(sigs, 5, i1, i2, i3, i4, i5); + #else + printf("%s, %s, %s, %s, %s\n", sigs[i1].word, sigs[i2].word, sigs[i3].word, sigs[i4].word, sigs[i5].word); + #endif #if FIND_ONE goto loopend; @@ -107,5 +133,9 @@ void findWords(signature* sigs, uint32_t len) { #if FIND_ONE loopend: #endif + END_TIME + #if FREE + free(starts); + #endif return; } \ No newline at end of file