wordle-algorithm/solvedle.c

141 lines
3.8 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-11 19:04:40 -04:00
#if PRINT_INTERMEDIATES
extern uint32_t rawWC;
#endif
int main(int argc, char * argv[]) {
#if TIME_SECTIONS
clock_t prog_start = clock();
#endif
FILE * f;
#if ASSUME_SAFE
if (argc != 2) {
fprintf(stderr, "usage: %s file\n", argv[0]);
return 2;
}
f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "%s: %s: no such file or directory\n", argv[0], argv[1]);
return 2;
}
#else
f = fopen(argv[1], "r");
#endif
START_TIME("making array")
uint32_t fiveUniqWC;
signature* sigs = makeArray(f, &fiveUniqWC);
END_TIME
fclose(f);
#if PRINT_INTERMEDIATES
fprintf(stderr, "INTERMEDIATE: read %u words\n", rawWC);
fprintf(stderr, "INTERMEDIATE: kept %u words with 5 unique letters\n", i);
#endif
#if DELETE
START_TIME("sorting")
qsort(sigs, fiveUniqWC, sizeof(signature), compare);
END_TIME
START_TIME("removing duplicates")
uint32_t cookedWC = rmdups(sigs, fiveUniqWC);
END_TIME
#else
uint32_t cookedWC = fiveUniqWC;
#endif
#if PRINT_INTERMEDIATES
fprintf(stderr, "INTERMEDIATE: ended with %u totally unique words\n", cookedWC);
#endif
findWords(sigs, cookedWC);
#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
2022-09-23 16:11:08 -04:00
#if FREE
for (uint32_t i = 0; i < fiveUniqWC; i++) {
free(sigs[i].word);
}
free(sigs);
#endif
2022-09-11 19:04:40 -04:00
return 0;
}
2022-09-03 15:23:40 -04:00
2022-09-10 14:03:23 -04:00
void findWords(signature* sigs, uint32_t len) {
2022-09-23 16:11:08 -04:00
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
2022-09-11 19:04:40 -04:00
2022-09-23 16:11:08 -04:00
START_TIME("finding words")
// the big loop.
2022-09-10 14:03:23 -04:00
for (uint32_t i1 = 0; i1 < len; i1++) {
uint32_t a = sigs[i1].sign;
2022-09-03 15:23:40 -04:00
2022-09-23 16:11:08 -04:00
for (uint32_t i2 = starts[i1]+i1; i2 < len; i2++) {
2022-09-10 14:03:23 -04:00
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-23 16:11:08 -04:00
for (uint32_t i3 = starts[i2]+i2; i3 < len; i3++) {
2022-09-10 14:03:23 -04:00
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-23 16:11:08 -04:00
for (uint32_t i4 = starts[i3]+i3; i4 < len; i4++) {
2022-09-10 14:03:23 -04:00
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-23 16:11:08 -04:00
for (uint32_t i5 = starts[i4]+i4; i5 < len; i5++) {
2022-09-10 14:03:23 -04:00
uint32_t e = sigs[i5].sign;
if (abcd & e) { continue; }
2022-09-23 16:11:08 -04:00
#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
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
2022-09-23 16:11:08 -04:00
END_TIME
#if FREE
free(starts);
#endif
2022-09-10 14:03:23 -04:00
return;
2022-09-03 13:58:20 -04:00
}