Skip to content

Commit

Permalink
Fix remaining warnings
Browse files Browse the repository at this point in the history
Also, remove no-longer-needed PGO (profile-guided optimization).

That helped a lot with the old O(N^3) algorithm but really isn't
needed with O(N^2) algorithm since a542e18. :-)
  • Loading branch information
dlenski committed Apr 16, 2023
1 parent 5b54522 commit c97338e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45 deletions.
38 changes: 2 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,12 @@
# 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

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
22 changes: 13 additions & 9 deletions best_guess.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@
*
*/

#define _GNU_SOURCE
#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
Expand All @@ -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

/***************************************************************************/

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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...
Expand All @@ -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]++;
}
Expand Down

0 comments on commit c97338e

Please sign in to comment.