first commit

master
Nicholas Hope 2022-09-03 13:58:20 -04:00
commit e2e00c9388
4 changed files with 13146 additions and 0 deletions

10
Makefile Normal file
View File

@ -0,0 +1,10 @@
printing ?= 0
.PHONY: default
.DEFAULT: default
default: solvedle.c
@clang -o bw -Ofast -Dprint=$(printing) $< && ./bw abcde.txt
.PHONY: test
test: solvedle.c
@clang -o bw -Ofast -Dprint=$(printing) $< && ./bw wordleWords.txt

13
abcde.txt Normal file
View File

@ -0,0 +1,13 @@
abcde
abcdf
abcdg
fghij
fghik
fghil
klmno
klmnp
qabcd
qrstu
qrstv
vaxyz
vwxyz

149
solvedle.c Normal file
View File

@ -0,0 +1,149 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define WORDS 12971
#define WORDLEN 5
#define CHARMASK ~(0b11u << 5u)
typedef struct {
char * word;
uint32_t sign;
} signature;
uint32_t
rawWC = 0,
fiveUniqWC = 0,
cookedWC = 0
;
int compare(const void * a, const void * b) {
return (((signature *)a)->sign) - (((signature *)b)->sign);
}
void * rmdups(signature * arr) {
// list is already sorted
uint32_t nitems = fiveUniqWC;
for (uint32_t i = 0; i < nitems; /* handled in loop */) {
if (arr[i].sign == arr[i + 1].sign) {
memmove( &arr[ i ], &arr[ i + 1 ], (nitems - i) * sizeof(signature));
nitems--;
}
else {
i++;
}
}
cookedWC = nitems;
return arr;
// return realloc(arr, cookedWC * sizeof(signature));
}
uint32_t getsig(char * word) {
uint32_t result = 0;
for (uint32_t i = 0; i < 5; i++) {
uint32_t bit = 1 << (word[i] & CHARMASK);
if (result & bit) {
// duplicate letters
return 0;
}
result |= bit;
}
return result;
}
void * makeArray(FILE * f) {
signature* sigs = (signature*) calloc(WORDS, sizeof(signature));
char word[WORDLEN + sizeof(char)]; // 5 letters + \n
uint32_t charsRead;
while (fread(word, sizeof(char), WORDLEN + sizeof(char), f) != 0) {
rawWC++;
// only assign sign so far, more efficient
uint32_t s = getsig(word);
if (s != 0) {
sigs[fiveUniqWC].word = strndup(word, 5);
sigs[fiveUniqWC].sign = s;
fiveUniqWC++;
}
}
// return realloc(sigs, fiveUniqWC * sizeof(signature));
return sigs;
}
int main(int argc, char * argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s file\n", argv[0]);
return 2;
}
FILE * f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "%s: %s: no such file or directory\n", argv[0], argv[1]);
return 2;
}
signature* sigs = (signature*) makeArray(f);
fclose(f);
printf("read %u words\n", rawWC);
printf("kept %u words with 5 unique letters\n", fiveUniqWC);
qsort(sigs, fiveUniqWC, sizeof(signature), compare);
sigs = (signature*) rmdups(sigs);
printf("ended with %u totally unique words\n", cookedWC);
#if 0
for (uint32_t i = 0; i < cookedWC; i++) {
printf("%u\n", sigs[i].sign);
}
#else
for (uint32_t i1 = 0; i1 < cookedWC; i1++) {
uint32_t a = sigs[i1].sign;
for (uint32_t i2 = i1+1; i2 < cookedWC; i2++) {
uint32_t b = sigs[i2].sign;
if (a & b) {
continue;
}
uint32_t ab = a | b;
#if print
fprintf(stderr, "%s:%s\n", sigs[i1].word, sigs[i2].word);
#endif
for (uint32_t i3 = i2+1; i3 < cookedWC; i3++) {
uint32_t c = sigs[i3].sign;
if (ab & c) {
continue;
}
uint32_t abc = ab | c;
#if print
fprintf(stderr, "%s:%s:%s\n", sigs[i1].word, sigs[i2].word, sigs[i3].word);
#endif
for (uint32_t i4 = i3+1; i4 < cookedWC; i4++) {
uint32_t d = sigs[i4].sign;
if (abc & d) {
continue;
}
uint32_t abcd = abc | d;
#if print
fprintf(stderr, "%s:%s:%s:%s\n", sigs[i1].word, sigs[i2].word, sigs[i3].word, sigs[i4].word);
#endif
for (uint32_t i5 = i4+1; i5 < cookedWC; i5++) {
uint32_t e = sigs[i5].sign;
if (abcd & e) {
continue;
}
fprintf(stderr, "%s, %s, %s, %s, %s\n", sigs[i1].word, sigs[i2].word, sigs[i3].word, sigs[i4].word, sigs[i5].word);
// goto loopend;
}
}
}
}
}
// loopend:
#endif
return 0;
}

12974
wordleWords.txt Normal file

File diff suppressed because it is too large Load Diff