Optimized below 2 seconds

master
Nicholas Hope 2022-09-23 17:20:01 -04:00
parent 2462e543fa
commit 7757d1a5eb
3 changed files with 19 additions and 14 deletions

View File

@ -26,9 +26,9 @@ free ?= 0
# I do not recommend setting it to false. # I do not recommend setting it to false.
delete ?= 1 delete ?= 1
CC := clang CC := clang
MACROS := -DPRINT_INTERMEDIATES=$(printing) -DTIME_SECTIONS=$(timing) -Dassume=$(assume) -DFIND_ONE=$(find_one) -DDELETE=$(delete) -DFANCYPRINT=$(fancy) -DFREE=$(free) MACROS := -DPRINT_INTERMEDIATES=$(printing) -DTIME_SECTIONS=$(timing) -DASSUME_SAFE=$(assume) -DFIND_ONE=$(find_one) -DDELETE=$(delete) -DFANCYPRINT=$(fancy) -DFREE=$(free)
FLAGS := -Ofast -Wall -Werror FLAGS := -Ofast -Wall -Werror
OUTPUT := -o bw OUTPUT := -o bw
@ -52,7 +52,7 @@ run: bw wordleWords.txt
.PHONY: fast .PHONY: fast
fast: solvedle.c boilerplate.c fast: solvedle.c boilerplate.c
$(CC) $(FLAGS) -DPRINT_INTERMEDIATES=0 -DTIME_SECTIONS=0 -Dassume=1 -DFIND_ONE=1 -DDELETE=1 -DFANCYPRINT=0 -DFREE=0 -c $^ $(CC) $(FLAGS) -DPRINT_INTERMEDIATES=0 -DTIME_SECTIONS=0 -DASSUME_SAFE=1 -DFIND_ONE=1 -DDELETE=1 -DFANCYPRINT=0 -DFREE=0 -c $^
make bw make bw
.PHONY: clean .PHONY: clean

View File

@ -37,7 +37,7 @@ uint32_t rawWC = 0;
#define CHARMASK ~0b1100000u #define CHARMASK ~0b1100000u
#define A_BIT 0b10u #define A_BIT 0b10u
#define Z_BIT (A_BIT << 26) #define Z_BIT (A_BIT << 26)
#define MAX(a,b) (a > b) ?a :b
#if PRINT_INTERMEDIATES #if PRINT_INTERMEDIATES
#if FANCYPRINT #if FANCYPRINT

View File

@ -77,42 +77,45 @@ void findWords(signature* sigs, uint32_t len) {
// for each index, locate the first index of // for each index, locate the first index of
// a word that shares no letters with it, and // a word that shares no letters with it, and
// store the distance between the two. // store the distance between the two.
uint32_t endindex = 0;
uint32_t * starts = calloc(len, sizeof(uint32_t)); uint32_t * starts = calloc(len, sizeof(uint32_t));
for (uint32_t i = 0, j = 0; i < len && j < len; i++) { for (uint32_t i = 0; endindex < len && i < len; endindex++) {
uint32_t sig = sigs[i].sign; uint32_t sig = sigs[endindex].sign;
for (j = i+1; j < len; j++) { for (i = endindex+1; i < len; i++) {
if ((sig & sigs[j].sign) == 0) { if ((sig & sigs[i].sign) == 0) {
starts[i] = j - i; starts[endindex] = i - endindex;
break; break;
} }
} }
} }
END_TIME END_TIME
#define START(var) starts[var] + var
START_TIME("finding words") START_TIME("finding words")
// the big loop. // the big loop.
for (uint32_t i1 = 0; i1 < len; i1++) { for (uint32_t i1 = 0; i1 < endindex; i1++) {
uint32_t a = sigs[i1].sign; uint32_t a = sigs[i1].sign;
for (uint32_t i2 = starts[i1]+i1; i2 < len; i2++) { for (uint32_t i2 = START(i1); i2 < endindex; i2++) {
uint32_t b = sigs[i2].sign; uint32_t b = sigs[i2].sign;
if (a & b) { continue; } if (a & b) { continue; }
uint32_t ab = a | b; uint32_t ab = a | b;
PRINT2 PRINT2
for (uint32_t i3 = starts[i2]+i2; i3 < len; i3++) { for (uint32_t i3 = START(i2); i3 < endindex; i3++) {
uint32_t c = sigs[i3].sign; uint32_t c = sigs[i3].sign;
if (ab & c) { continue; } if (ab & c) { continue; }
uint32_t abc = ab | c; uint32_t abc = ab | c;
PRINT3 PRINT3
for (uint32_t i4 = starts[i3]+i3; i4 < len; i4++) { for (uint32_t i4 = START(i3); i4 < endindex; i4++) {
uint32_t d = sigs[i4].sign; uint32_t d = sigs[i4].sign;
if (abc & d) { continue; } if (abc & d) { continue; }
uint32_t abcd = abc | d; uint32_t abcd = abc | d;
PRINT4 PRINT4
for (uint32_t i5 = starts[i4]+i4; i5 < len; i5++) { uint32_t i5start = START(i4);
for (uint32_t i5 = MAX(endindex, i5start); i5 < len; i5++) {
uint32_t e = sigs[i5].sign; uint32_t e = sigs[i5].sign;
if (abcd & e) { continue; } if (abcd & e) { continue; }
#if FANCYPRINT #if FANCYPRINT
@ -130,6 +133,8 @@ void findWords(signature* sigs, uint32_t len) {
} }
} }
} }
#undef START
#if FIND_ONE #if FIND_ONE
loopend: loopend:
#endif #endif