Crossword2

Continuation of Crossword exercise See also Histogram o' WordLengths

A version that counts the number of possible crossings (see sample output below): code format="c" $ cat xw4.c
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) define OCElementType char *
 * 5) include "OrderedCollection.c"

char *words[100000];   // This pair is a de-facto structure int wordCount=0;         // An OrderedCollection would be the data structure of choice

printWord(int ix) { printf("word[%i]=\"%s\"\n", ix, words[ix]); }

int findAny(char seek, int lix, int wordLen) { // answer the index of the first one found, -1 if none int ix; for(ix=0; ix<wordCount; ix++) { if(strlen(words[ix]) == wordLen) { // printf("ix=%i len=%i lix=%i seek=%c word=%s.\n",ix, wordLen, lix, seek, words[ix]); if ((words[ix])[lix] == seek) return(ix); }   }    return(-1); }

struct OrderedCollection *findAllSuchThat(char seek, int lix, int wordLen) { int ix, cnt=1; struct OrderedCollection *rslt; rslt = newOC; for(ix=0; ix<wordCount; ix++) { if(strlen(words[ix]) == wordLen) { if ((words[ix])[lix] == seek) { //               printf("(%i/%i=%c) %4i:",lix, wordLen, seek, cnt++); //               printWord(ix); addLast(rslt, words[ix]); }       }    }    return(rslt); }

printAllSuchThat(char seek, int lix, int wordLen) { // use when at least one has been found int ix, cnt=1; for(ix=0; ix 0) { if (findAny(ch,lix2,wordLen2) > 0) { fnd1 = findAllSuchThat(ch,lix1,wordLen1); fnd2 = findAllSuchThat(ch,lix2,wordLen2); sz1 = sizeOC(fnd1); sz2 = sizeOC(fnd2); total += (combo = sz1 * sz2); printf("%c == %i/%i (%i) == %i/%i (%i)\t\t#=%i total=%i \n", ch, lix1, wordLen1, sz1, lix2, wordLen2, sz2, combo, total); free(fnd1); free(fnd2); }       }    }    return(total); }

main{ FILE *inFile; char bufr[30]; int sz, fix; struct OrderedCollection *found;

inFile=fopen("/usr/share/dict/american-english","r"); printf("My file = %i\n",inFile);

while (fgets(bufr,30,inFile)!=0) { if (!isupper (bufr[0]) ) { bufr[(sz = strlen(bufr)) -1] = '\0'; words[wordCount] = malloc(sz); strcpy(words[wordCount], bufr); wordCount++; }   }    printf("Read %i words\n", wordCount); //   printWord(0); //   printWord(1); //   printWord(500); printWord(wordCount-1);

//   fix = findAny('b', 3, 5); //   printf("fix=%i ", fix); //   if (fix > 0) printWord(fix); //   fix = findAny('x', 2, 5); printf("fix=%i ", fix); if (fix > 0) printAllSuchThat('x', 2, 5);

found = findAllSuchThat('x', 2, 5); printf("found %i solutions\n", sizeOC(found)); countCrosses(1,11, 9,13);

}

// start modestly... // find word pairs where the 7th of 9 letters in the vertical word is the // same as the 5th of 8 letters of the horizontal word // use /usr/share/dict/american-english // // step-wise improvements... // parameterize: letter mix of mlen = nix of nlen // // + Omit proper nouns (done: omitted capitalized words) // Omit possessives: A zircon's shape is a cube. // last few entries which do not look like regular word? Keep those: letters w/ accents as in etude // // migrate common code out of loops // // make it fast: //     use data structures to prepare for answering MANY such questions

Tail of the output: a == 1/11 (861) == 9/13 (242)       #=208362  total=208362 b == 1/11 (61) == 9/13 (11)       #=671  total=209033 c == 1/11 (120) == 9/13 (141)       #=16920  total=225953 d == 1/11 (61) == 9/13 (36)       #=2196  total=228149 e == 1/11 (1202) == 9/13 (205)       #=246410  total=474559 f == 1/11 (40) == 9/13 (9)       #=360  total=474919 g == 1/11 (25) == 9/13 (45)       #=1125  total=476044 h == 1/11 (268) == 9/13 (46)       #=12328  total=488372 i == 1/11 (666) == 9/13 (303)       #=201798  total=690170 k == 1/11 (12) == 9/13 (14)       #=168  total=690338 l == 1/11 (274) == 9/13 (79)       #=21646  total=711984 m == 1/11 (161) == 9/13 (46)       #=7406  total=719390 n == 1/11 (682) == 9/13 (319)       #=217558  total=936948 o == 1/11 (1001) == 9/13 (359)       #=359359  total=1296307 p == 1/11 (135) == 9/13 (30)       #=4050  total=1300357 r == 1/11 (649) == 9/13 (111)       #=72039  total=1372396 s == 1/11 (77) == 9/13 (243)       #=18711  total=1391107 t == 1/11 (170) == 9/13 (454)       #=77180  total=1468287 u == 1/11 (468) == 9/13 (81)       #=37908  total=1506195 v == 1/11 (78) == 9/13 (42)       #=3276  total=1509471 w == 1/11 (26) == 9/13 (13)       #=338  total=1509809 y == 1/11 (101) == 9/13 (11)       #=1111  total=1510920

code