wordle-algorithm/solvedle.c

129 lines
3.2 KiB
C
Raw Normal View History

2022-09-03 13:58:20 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
2022-09-10 14:03:23 -04:00
#include "boilerplate.h"
2022-09-03 13:58:20 -04:00
2022-09-10 14:03:23 -04:00
uint32_t
2022-09-10 14:16:43 -04:00
rawWC,
fiveUniqWC
2022-09-10 14:03:23 -04:00
;
2022-09-03 15:23:40 -04:00
2022-09-10 14:03:23 -04:00
void findWords(signature* sigs, uint32_t len) {
for (uint32_t i1 = 0; i1 < len; i1++) {
uint32_t a = sigs[i1].sign;
2022-09-03 15:23:40 -04:00
2022-09-10 14:03:23 -04:00
for (uint32_t i2 = i1+1; i2 < len; i2++) {
uint32_t b = sigs[i2].sign;
if (a & b) { continue; }
uint32_t ab = a | b;
PRINT2
2022-09-03 15:23:40 -04:00
2022-09-10 14:03:23 -04:00
for (uint32_t i3 = i2+1; i3 < len; i3++) {
uint32_t c = sigs[i3].sign;
if (ab & c) { continue; }
uint32_t abc = ab | c;
PRINT3
2022-09-03 15:23:40 -04:00
2022-09-10 14:03:23 -04:00
for (uint32_t i4 = i3+1; i4 < len; i4++) {
uint32_t d = sigs[i4].sign;
if (abc & d) { continue; }
uint32_t abcd = abc | d;
PRINT4
2022-09-03 13:58:20 -04:00
2022-09-10 14:03:23 -04:00
for (uint32_t i5 = i4+1; i5 < len; i5++) {
uint32_t e = sigs[i5].sign;
if (abcd & e) { continue; }
printf("%s, %s, %s, %s, %s\n", sigs[i1].word, sigs[i2].word, sigs[i3].word, sigs[i4].word, sigs[i5].word);
2022-09-03 15:23:40 -04:00
2022-09-10 14:03:23 -04:00
#if FIND_ONE
goto loopend;
#endif
2022-09-03 13:58:20 -04:00
2022-09-10 14:03:23 -04:00
}
}
}
}
}
#if FIND_ONE
loopend:
#endif
return;
2022-09-03 13:58:20 -04:00
}
2022-09-03 15:23:40 -04:00
signature* makeArray(FILE * f) {
2022-09-03 13:58:20 -04:00
signature* sigs = (signature*) calloc(WORDS, sizeof(signature));
char word[WORDLEN + sizeof(char)]; // 5 letters + \n
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) {
2022-09-03 15:23:40 -04:00
// no duplicate word letters in word
2022-09-03 13:58:20 -04:00
sigs[fiveUniqWC].word = strndup(word, 5);
sigs[fiveUniqWC].sign = s;
fiveUniqWC++;
}
}
return sigs;
}
int main(int argc, char * argv[]) {
2022-09-03 15:23:40 -04:00
#if TIME_SECTIONS
clock_t prog_start = clock();
#endif
#if ! ASSUME_SAFE
2022-09-03 13:58:20 -04:00
if (argc != 2) {
fprintf(stderr, "usage: %s file\n", argv[0]);
return 2;
}
2022-09-03 15:23:40 -04:00
#endif
2022-09-03 13:58:20 -04:00
FILE * f = fopen(argv[1], "r");
2022-09-03 15:23:40 -04:00
#if ! ASSUME_SAFE
2022-09-03 13:58:20 -04:00
if (f == NULL) {
2022-09-10 14:03:23 -04:00
fprintf(stderr, "%s, %s, no such file or directory\n", argv[0], argv[1]);
2022-09-03 13:58:20 -04:00
return 2;
}
2022-09-03 15:23:40 -04:00
#endif
START_TIME("making array")
signature* sigs = makeArray(f);
END_TIME
2022-09-03 13:58:20 -04:00
fclose(f);
2022-09-03 15:23:40 -04:00
#if PRINT_INTERMEDIATES
2022-09-10 14:03:23 -04:00
fprintf(stderr, "INTERMEDIATE: read %u words\n", rawWC);
fprintf(stderr, "INTERMEDIATE: kept %u words with 5 unique letters\n", fiveUniqWC);
2022-09-03 15:23:40 -04:00
#endif
2022-09-03 13:58:20 -04:00
2022-09-10 14:03:23 -04:00
#if DELETE
2022-09-03 15:23:40 -04:00
START_TIME("sorting")
2022-09-03 13:58:20 -04:00
qsort(sigs, fiveUniqWC, sizeof(signature), compare);
2022-09-03 15:23:40 -04:00
END_TIME
START_TIME("removing duplicates")
2022-09-10 14:16:43 -04:00
uint32_t cookedWC = rmdups(sigs, fiveUniqWC);
2022-09-03 15:23:40 -04:00
END_TIME
2022-09-10 14:03:23 -04:00
#else
2022-09-10 14:16:43 -04:00
uint32_t cookedWC = fiveUniqWC;
2022-09-03 13:58:20 -04:00
#endif
2022-09-03 15:23:40 -04:00
#if PRINT_INTERMEDIATES
2022-09-10 14:03:23 -04:00
fprintf(stderr, "INTERMEDIATE: ended with %u totally unique words\n", cookedWC);
2022-09-03 15:23:40 -04:00
#endif
2022-09-10 14:03:23 -04:00
START_TIME("finding words")
findWords(sigs, cookedWC);
2022-09-03 15:23:40 -04:00
END_TIME
#if TIME_SECTIONS
clock_t prog_end = clock();
2022-09-10 14:03:23 -04:00
fprintf(stderr, "%s took %fs total\n", argv[0], ((double)(prog_end - prog_start)) / CLOCKS_PER_SEC);
2022-09-03 13:58:20 -04:00
#endif
return 0;
}