// // Skeyways Crosswords // #include #include #include const int FALSE = 0; const int TRUE = !FALSE; // Function to check if c is upper case int isupper(char c) { return c >= 'A' && c <= 'Z'; } FindUpper(char word[]) { for(int i = 0; word[i]; i++) if(isupper(word[i])) break; if(word[i] == 0) { cerr << "Error invalid word.\n"; exit(1); } else word[i] -= 'A' - 'a'; // convert to lower case return i; } FindLetter(char word[], char c) { for(int i = 0; word[i]; i++) if(word[i] == c) break; if(word[i] == 0) cerr << "Error invalid word.\n"; return i; } main() { ifstream in("day2b.dat", ios::nocreate); if(in == NULL) { cout << "error no input file\n"; return 1; // error return } // x, y point to the coordinates of the current word. int x = 0, y = 0; int top = 0, bottom = 0, left = 0, right = 0; char OldWord[1000], NewWord[1000]; // Get in the first word in >> OldWord; int len = strlen(OldWord); // initial crossword size (holds the first word) bottom = 1; right = len; int across = FALSE; in >> NewWord; while(NewWord[0] != '0') { int pos = FindUpper(NewWord); len = strlen(NewWord); if(across) { // find the position of the start of this word x = x - pos; // Go up from the current position y += FindLetter(OldWord, NewWord[pos]); // Go across if(x < left) left = x; if(x + len > right) right = x + len; } else { // find the position of the start of this word y = y - pos; // Go up from the current position x += FindLetter(OldWord, NewWord[pos]); // Go across if(y < top) top = y; if(y + len > bottom) bottom = y + len; // This increases crossword size. } // Get ready for next word ... across = !across; // ... change direction strcpy(OldWord, NewWord); // ... new becomes old in >> NewWord; // ... get the word } ofstream out("day2b.sol"); out << right - left << endl; out << bottom - top << endl; return 0; }