/**
* @author Gansito Frito
*/
package solver;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
/**
* @author Gansito Frito
*
*/
public class Solver {
private File wordsearch;
private File dictionary;
private int numLines;
private int lineLength;
private Vector<Vector<Character>> wordSearch = new Vector<Vector<Character>>();
private List<String> dictionaryWords = new ArrayList<String>();
private Map<String, PairIntPair> stringMap = new HashMap<String, PairIntPair>();
public Solver(File wordsearch, File dictionary) {
this.wordsearch = wordsearch;
this.dictionary = dictionary;
}
public void run() throws Exception {
initializeWordSearch();
readDictionary();
long beginTime = System.currentTimeMillis();
initializeWordSearchStrings();
compareDictionary();
long endTime = System.currentTimeMillis();
System.out.println(endTime - beginTime);
}
private void compareDictionary() throws Exception {
FileOutputStream stream = new FileOutputStream(new File(
"outputWords.txt"));
PrintStream pstream = new PrintStream(stream);
Iterator<String> iter = stringMap.keySet().iterator();
while (iter.hasNext()) {
String currentString = iter.next();
if (dictionaryWords.contains(currentString)) {
PairIntPair pairHere = stringMap.get(currentString);
String toWrite = currentString + " from: "
+ pairHere.unIndexToString();
pstream.println(toWrite);
}
}
}
private void readDictionary() throws Exception {
FileReader fileReader = new FileReader(dictionary);
BufferedReader reader = new BufferedReader(fileReader);
while (reader.ready()) {
dictionaryWords.add(reader.readLine());
}
reader.close();
fileReader.close();
}
private void initializeWordSearch() throws Exception {
FileReader fileReader = new FileReader(wordsearch);
BufferedReader reader = new BufferedReader(fileReader);
int count = -1;
int lineLength = -1;
while (reader.ready()) {
count++;
String str = reader.readLine();
if (lineLength != str.toCharArray().length && count != 0) {
throw new IllegalStateException(
"Wordsearch must be rectangular");
}
lineLength = str.toCharArray().length;
wordSearch.add(count, new Vector<Character>());
for (int i = 0; i < lineLength; i++) {
wordSearch.get(count).add(str.toCharArray()[i]);
}
}
numLines = count + 1;
this.lineLength = lineLength;
reader.close();
fileReader.close();
}
private void initializeWordSearchStrings() {
getDiagonalStrings();
getHorizontalStrings();
getVerticalStrings();
}
private void getVerticalStrings() {
int zeroIndexNumLines = numLines - 1;
Iterator<Vector<Character>> iter = wordSearch.iterator();
Integer vectorIndex = -1;
while (iter.hasNext()) {
Vector<Character> currentVector = iter.next();
vectorIndex++;
Integer vertDiff = zeroIndexNumLines - vectorIndex;
Iterator<Character> charIter = currentVector.iterator();
Integer characterIndex = -1;
while (charIter.hasNext()) {
Character currentCharacter = charIter.next();
characterIndex++;
for (int i = 0; i <= vertDiff; i++) {
IntPair a = new IntPair(vectorIndex, characterIndex);
IntPair b = new IntPair(vectorIndex + i, characterIndex);
PairIntPair pair = new PairIntPair(a, b);
PairIntPair backpair = new PairIntPair(b, a);
char[] array = new char[i + 1];
char[] backarray = new char[i + 1];
array[0] = currentCharacter;
backarray[i] = currentCharacter;
for (int j = 1; j <= i; j++) {
array[j] = wordSearch.get(vectorIndex + j).get(
characterIndex);
backarray[i - j] = array[j];
}
String str = new String(array);
String backstr = new String(backarray);
stringMap.put(str, pair);
stringMap.put(backstr, backpair);
}
}
}
}
private void getHorizontalStrings() {
int zeroIndexLineLength = lineLength - 1;
Iterator<Vector<Character>> iter = wordSearch.iterator();
Integer vectorIndex = -1;
while (iter.hasNext()) {
Vector<Character> currentVector = iter.next();
vectorIndex++;
Iterator<Character> charIter = currentVector.iterator();
Integer characterIndex = -1;
while (charIter.hasNext()) {
Character currentCharacter = charIter.next();
characterIndex++;
Integer horizDiff = zeroIndexLineLength - characterIndex;
for (int i = 0; i <= horizDiff; i++) {
IntPair a = new IntPair(vectorIndex, characterIndex);
IntPair b = new IntPair(vectorIndex, characterIndex + i);
PairIntPair pair = new PairIntPair(a, b);
PairIntPair backpair = new PairIntPair(b, a);
char[] array = new char[i + 1];
char[] backarray = new char[i + 1];
array[0] = currentCharacter;
backarray[i] = currentCharacter;
for (int j = 1; j <= i; j++) {
array[j] = wordSearch.get(vectorIndex).get(
characterIndex + j);
backarray[i - j] = array[j];
}
String str = new String(array);
String backstr = new String(backarray);
stringMap.put(str, pair);
stringMap.put(backstr, backpair);
}
}
}
}
private void getDiagonalStrings() {
int zeroIndexLineLength = lineLength - 1;
int zeroIndexNumLines = numLines - 1;
Iterator<Vector<Character>> iter = wordSearch.iterator();
Integer vectorIndex = -1;
while (iter.hasNext()) {
Vector<Character> currentVector = iter.next();
vectorIndex++;
Integer vertDiff = zeroIndexNumLines - vectorIndex;
Iterator<Character> charIter = currentVector.iterator();
Integer characterIndex = -1;
while (charIter.hasNext()) {
Character currentCharacter = charIter.next();
characterIndex++;
Integer horizDiff = zeroIndexLineLength - characterIndex;
for (int i = 0; i <= horizDiff && i <= vertDiff; i++) {
IntPair a = new IntPair(vectorIndex, characterIndex);
IntPair b = new IntPair(vectorIndex + i, characterIndex + i);
PairIntPair pair = new PairIntPair(a, b);
PairIntPair backpair = new PairIntPair(b, a);
char[] array = new char[i + 1];
char[] backarray = new char[i + 1];
array[0] = currentCharacter;
backarray[i] = currentCharacter;
for (int j = 1; j <= i; j++) {
array[j] = wordSearch.get(vectorIndex + j).get(
characterIndex + j);
backarray[i - j] = array[j];
}
String str = new String(array);
String backstr = new String(backarray);
stringMap.put(str, pair);
stringMap.put(backstr, backpair);
}
for (int i = 1; i <= horizDiff && i <= vectorIndex; i++) {
IntPair a = new IntPair(vectorIndex, characterIndex);
IntPair c = new IntPair(vectorIndex - i, characterIndex + i);
PairIntPair pair = new PairIntPair(a, c);
PairIntPair backpair = new PairIntPair(c, a);
char[] array = new char[i + 1];
char[] backarray = new char[i + 1];
array[0] = currentCharacter;
backarray[i] = currentCharacter;
for (int j = 1; j <= i; j++) {
array[j] = wordSearch.get(vectorIndex - j).get(
characterIndex + j);
backarray[i - j] = array[j];
}
String str = new String(array);
String backstr = new String(backarray);
stringMap.put(str, pair);
stringMap.put(backstr, backpair);
}
}
}
}
}