diff --git a/Makefile b/Makefile index da40637..2f6008a 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # set CC = gcc-11 CC = gcc -CFLAGS = -std=c11 -O3 -flto=auto -fwhole-program #-Wall +CFLAGS = -std=c11 -O3 -flto=auto -fwhole-program -Wall FULL_WORDLIST = /usr/share/dict/words #FULL_WORDLIST = wordle_answerlist.txt @@ -10,38 +10,4 @@ best_guess: best_guess.c ${CC} $(CFLAGS) best_guess.c -o best_guess clean: - -@rm best_guess pgo-is-generated 50_5char_wordlist 500_5char_wordlist 500_6char_wordlist 500_7char_wordlist 1000_7char_wordlist 500_8char_wordlist 2>/dev/null || true - -pgo: pgo-is-generated - -pgo-is-generated: 50_5char_wordlist - ${CC} -fprofile-generate $(CFLAGS) best_guess.c -o best_guess - ./best_guess 50_5char_wordlist 5 > /dev/null 2>&1 - ./best_guess 50_5char_wordlist 5 550_5char_guesslist > /dev/null 2>&1 - ${CC} -fprofile-use $(CFLAGS) best_guess.c -o best_guess - touch pgo-is-generated - -time: 500_5char_wordlist pgo-is-generated - time ./best_guess 500_5char_wordlist 5 > /dev/null 2>&1 - -50_5char_wordlist: - grep -E '^[[:alpha:]]{5}$$' /usr/share/dict/words | shuf -n50 > 50_5char_wordlist - -500_5char_wordlist: - grep -E '^[[:alpha:]]{5}$$' /usr/share/dict/words | shuf -n500 > 500_5char_wordlist - -# almost 550 -550_5char_guesslist: 50_5char_wordlist 500_5char_wordlist - cat 50_5char_wordlist 500_5char_wordlist | sort | uniq > 550_5char_guesslist - -500_6char_wordlist: - grep -E '^[[:alpha:]]{6}$$' /usr/share/dict/words | shuf -n500 > 500_6char_wordlist - -500_7char_wordlist: - grep -E '^[[:alpha:]]{7}$$' /usr/share/dict/words | shuf -n500 > 500_7char_wordlist - -1000_7char_wordlist: - grep -E '^[[:alpha:]]{7}$$' /usr/share/dict/words | shuf -n1000 > 1000_7char_wordlist - -500_8char_wordlist: - grep -E '^[[:alpha:]]{8}$$' /usr/share/dict/words | shuf -n500 > 500_8char_wordlist + -@rm best_guess 2>/dev/null || true diff --git a/best_guess.c b/best_guess.c index bb93716..7e4d577 100644 --- a/best_guess.c +++ b/best_guess.c @@ -77,10 +77,12 @@ * */ +#define _GNU_SOURCE #include #include #include #include +#include #include #include #include @@ -100,10 +102,10 @@ const int N_LETTERS = 26; #define DEBUG_STDERR(...) fprintf (stderr, __VA_ARGS__) #define DEBUG_NOTHING(...) do{}while(0) -// Change any of these to DEBUG_STDERR(__VA_ARGS__) to enable, 0 to disable: -#define DEBUG_CLUES(...) -#define DEBUG_IS_WORD_POSSIBLE(...) -#define DEBUG_ELIGIBLE_WORDS(...) +// Change any of these to DEBUG_STDERR to enable, DEBUG_NOTHIN to disable: +#define DEBUG_CLUES DEBUG_NOTHING +#define DEBUG_IS_WORD_POSSIBLE DEBUG_NOTHING +#define DEBUG_ELIGIBLE_WORDS DEBUG_NOTHING /***************************************************************************/ @@ -171,7 +173,8 @@ inline static int is_word_possible_after_guess(int len, const char *guess, const bzero(left_word, N_LETTERS*sizeof(int)); for (int ii=0; ii < len; ii++) { - char gl = guess[ii], wl = word[ii], gl_off = gl-'A', wl_off = wl-'A', s = clues[ii]; + char gl = guess[ii], wl = word[ii], s = clues[ii]; + int gl_off = gl-'A', wl_off = wl-'A'; if ((gl == wl) && s != RightPosition) { DEBUG_IS_WORD_POSSIBLE("1) %d %c==%c but %c!=R\n", ii, gl, wl, s); return 0; // Guess and word share a letter which is NOT marked as RP in the guess @@ -274,7 +277,7 @@ int eligible_words(FILE *f, int len, char ***output) { // Make uppercase, count uppercase/lowercase int gotlower = 0, gotupper = 0; - for (char *p = start; *p; *p++) { + for (char *p = start; *p; p++) { if (*p >= 'a' && *p <= 'z') { gotlower++; *p = toupper(*p); @@ -295,7 +298,9 @@ int eligible_words(FILE *f, int len, char ***output) { assert(buf[0] != NULL); assert(buf[n-1] != NULL); // I don't understand realloc } DEBUG_ELIGIBLE_WORDS("buf[%d] = \"%s\"\n", n, start); - buf[n++] = strdup(start); + char *save = strndup(start, len); + assert(save); + buf[n++] = save; not_a_word: continue; @@ -385,7 +390,6 @@ int main(int argc, char **argv) { int n_cluniques = ipow(3, targetlen); int n_possible_cluniques = n_cluniques - targetlen; - char clues[targetlen + 1]; time_t tstart = time(NULL); // Try each guess word... @@ -400,7 +404,7 @@ int main(int argc, char **argv) { int clunique; // Figure out the clues, and the "clunique" category of that guess+target combo - char clues[targetlen]; + char clues[targetlen+1]; clues_of_guess(targetlen, guess, target, clues, &clunique); cluniques[clunique]++; }