Created
October 2, 2014 23:12
-
-
Save Unh0lyTigg/794453bc5fb54b98658a to your computer and use it in GitHub Desktop.
Word Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.unh0lytigg.wordsearch; | |
import java.util.List; | |
import java.util.Map; | |
import com.google.common.collect.Lists; | |
import com.google.common.collect.Maps; | |
public class Grid { | |
private Map<Character, List<GridLocation>> gridCache1 = Maps.newHashMap(); | |
private Map<GridLocation, Character> gridCache2 = Maps.newHashMap(); | |
private Map<GridLocation, Map<GridDirection, String>> gridCache3 = Maps.newHashMap(); | |
private List<Object[]> gridCache4 = Lists.newArrayList(); | |
private String checkingWord = ""; | |
public static Grid makeGrid(String[] lines) { | |
return new Grid(lines); | |
} | |
private Grid(String[] lines) { | |
for (int y = 0; y < lines.length; y++) { | |
lines[y] = lines[y].toLowerCase(); | |
for (int x = 0; x < lines[y].length(); x++) { | |
Character c = Character.valueOf(lines[y].charAt(x)); | |
GridLocation loc = new GridLocation(x, y); | |
if (!gridCache1.containsKey(c)) | |
gridCache1.put(c, Lists.<GridLocation>newArrayList()); | |
gridCache1.get(c).add(loc); | |
gridCache2.put(loc, c); | |
} | |
} | |
for (int y = 0; y < lines.length; y++) { | |
for (int x = 0; x < lines[y].length(); x++) { | |
GridLocation loc = new GridLocation(x, y); | |
gridCache3.put(loc, Maps.<GridDirection, String>newHashMap()); | |
GridDirection.valueList().stream().forEachOrdered(d -> { | |
String cacheValue = buildCacheLine(loc, d); | |
gridCache3.get(loc).put(d, cacheValue); | |
}); | |
} | |
} | |
} | |
private String buildCacheLine(GridLocation location, GridDirection direction) { | |
GridLocation l = location.clone(); | |
StringBuilder b = new StringBuilder(); | |
while (check(l)) { | |
b.append(gridCache2.get(l)); | |
l = l.getNeigbor(direction); | |
} | |
return b.toString(); | |
} | |
private boolean check(GridLocation location) { | |
return gridCache2.containsKey(location); | |
} | |
public synchronized void tryCacheMatch(String word) { | |
checkingWord = word.toLowerCase(); | |
List<Object> obj = Lists.newArrayList(); | |
obj.add(word); | |
Character c = Character.valueOf(checkingWord.charAt(0)); | |
gridCache1.get(c).stream().forEach(loc -> { | |
Map<GridDirection, String> a = gridCache3.get(loc); | |
GridDirection.valueList().stream().forEach(d -> { | |
if (a.get(d).startsWith(checkingWord)) { | |
obj.add(loc.getY()+1); //offset for readability, swapped x and y for readability too. | |
obj.add(loc.getX()+1); | |
obj.add(d); | |
} | |
}); | |
}); | |
checkingWord = ""; | |
if (obj.size() == 1) | |
obj.add("Not found"); | |
gridCache4.add(obj.toArray()); | |
} | |
public List<Object[]> getMatchResults() { | |
return Lists.newArrayList(gridCache4); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.unh0lytigg.wordsearch; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import com.google.common.base.Joiner; | |
import com.google.common.base.Splitter; | |
import com.google.common.collect.Lists; | |
@AllArgsConstructor | |
public enum GridDirection { | |
UP(0, -1), | |
UP_RIGHT(1, -1), | |
RIGHT(1, 0), | |
DOWN_RIGHT(1, 1), | |
DOWN(0, 1), | |
DOWN_LEFT(-1, 1), | |
LEFT(-1, 0), | |
UP_LEFT(-1, -1); // down here for order's sake | |
@Getter | |
private final int xOffset, yOffset; | |
private static final List<GridDirection> valueList = Collections.unmodifiableList(Arrays.asList(values())); | |
public static final List<GridDirection> valueList() { | |
return valueList; | |
} | |
@Override | |
public String toString() { | |
return Joiner.on(" & ").join(Lists.transform(Splitter.on('_').splitToList(name()), a -> { return a.charAt(0) + a.substring(1, a.length()).toLowerCase(); })); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.unh0lytigg.wordsearch; | |
import java.util.List; | |
import lombok.EqualsAndHashCode; | |
import lombok.Getter; | |
import lombok.ToString; | |
import com.google.common.base.Function; | |
import com.google.common.collect.Lists; | |
@EqualsAndHashCode | |
@ToString | |
public class GridLocation implements Cloneable { | |
@Getter | |
private int x, y; | |
public GridLocation(int x, int y) { | |
this.x = x; this.y = y; | |
} | |
public GridLocation getNeigbor(GridDirection direction) { | |
return new GridLocation(x + direction.getXOffset(), y + direction.getYOffset()); | |
} | |
public List<GridLocation> getNeighbors() { | |
return Lists.<GridDirection, GridLocation>transform(GridDirection.valueList(), new Function<GridDirection, GridLocation>() { | |
@Override | |
public GridLocation apply(GridDirection input) { | |
return GridLocation.this.getNeigbor(input); | |
} | |
}); | |
} | |
@Override | |
public GridLocation clone() { | |
return new GridLocation(x, y); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.unh0lytigg.wordsearch; | |
import java.util.Arrays; | |
import java.util.List; | |
import com.google.common.collect.Lists; | |
public class WordSearch { | |
public static void main(String[] args) { | |
// replace this with the letter grid | |
String[] lines = { | |
"akolimec", | |
"biregepa", | |
"fwamrlep", | |
"rinpaoar", | |
"ufglpnri", | |
"ineuelgc", | |
"tlemoneo", | |
"scherryt" | |
}; | |
// replace this with the words to search for | |
List<String> words = Lists.newArrayList("fruits", "apple", "orange", "mango", "apricot", "pear", "melon", "cherry", "kiwi", "lime", "plum", "lemon", "fig", "rr"); | |
Grid g = Grid.makeGrid(lines); | |
words.stream().forEachOrdered(g::tryCacheMatch); | |
System.out.println("Word\tRow\tColumn\tDirection"); | |
g.getMatchResults().stream().forEachOrdered(WordSearch::log); | |
} | |
public static void log(Object[] array) { | |
Arrays.asList(array).stream().forEach(WordSearch::logElement); | |
System.out.println(); | |
} | |
private static void logElement(Object o) { | |
System.out.print(o + "\t"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment