Last active
August 29, 2015 14:22
-
-
Save Yengas/883037f909225e299515 to your computer and use it in GitHub Desktop.
Boggle Solver
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 rec.probs; | |
| import java.io.BufferedReader; | |
| import java.io.FileInputStream; | |
| import java.io.InputStreamReader; | |
| import java.util.ArrayList; | |
| import java.util.Locale; | |
| public class WordSolver { | |
| public class Piece{ | |
| public final int row, column; | |
| public final char chr; | |
| public Piece(char chr, int row, int column){ | |
| this.row = row; | |
| this.column = column; | |
| this.chr = chr; | |
| } | |
| @Override | |
| public boolean equals(Object o){ | |
| if(o instanceof Piece == false) return false; | |
| Piece oth = (Piece) o; | |
| return oth.row == this.row && oth.column == this.column && oth.chr == this.chr; | |
| } | |
| @Override | |
| public String toString(){ | |
| return this.row + "," + this.column + "=" + this.chr; | |
| } | |
| } | |
| private final Piece[][] grid; | |
| public static final Locale locale = Locale.forLanguageTag("tr"); | |
| private final Piece[] adjacents = new Piece[8]; // For 4x4 | |
| public WordSolver(char[][] grid){ | |
| this.grid = new Piece[grid.length][]; | |
| for(int i = 0; i < grid.length; i++){ | |
| char[] row = grid[i]; | |
| this.grid[i] = new Piece[row.length]; | |
| int x = 0; | |
| for(char c : row){ | |
| this.grid[i][x] = new Piece(Character.toLowerCase(c), i, x); | |
| x++; | |
| } | |
| } | |
| } | |
| public Piece getPiece(int row, int column){ | |
| return isValid(row, column) ? this.grid[row][column] : null; | |
| } | |
| public boolean isValid(int row, int column){ | |
| return row >= 0 && row < grid.length && column >= 0 && column < grid[row].length; | |
| } | |
| public void fillAdjacents(Piece piece, Piece[] adjacents, ArrayList<Piece> exclude){ | |
| int index = 0; | |
| for(int column = piece.column - 1; column <= piece.column + 1; column++){ | |
| for(int row = piece.row - 1; row <= piece.row + 1; row++){ | |
| Piece adjacent = this.getPiece(row, column); | |
| if(adjacent != null && (row != piece.row || column != piece.column)){ | |
| if(exclude != null && exclude.contains(adjacent)) continue; | |
| adjacents[index++] = adjacent; | |
| } | |
| } | |
| } | |
| if(index < adjacents.length) adjacents[index] = null; | |
| } | |
| public void permutate(ArrayList<Piece> head, Piece piece){ | |
| if(head.size() == 0) head.add(piece); | |
| this.fillAdjacents(piece, adjacents, head); | |
| head.add(null); | |
| int index = 0, last = head.size() - 1; | |
| while(index < adjacents.length && adjacents[index] != null){ | |
| Piece adjacent = adjacents[index++]; | |
| head.set(last, adjacent); | |
| permutate(head, adjacent); | |
| } | |
| head.remove(last); | |
| } | |
| private boolean search(ArrayList<Piece> head, String word, Piece piece, int index){ | |
| if(index == word.length()) return true; | |
| if(head.size() == 0) head.add(piece); | |
| Piece[] adjacents = new Piece[8]; | |
| char cmp = word.charAt(index); | |
| this.fillAdjacents(piece, adjacents, head); | |
| head.add(null); | |
| int i = 0, last = head.size() - 1; | |
| while(i < adjacents.length && adjacents[i] != null){ | |
| Piece p = adjacents[i++]; | |
| if(p.chr == cmp){ | |
| head.set(last, p); | |
| if(search(head, word, p, index + 1)) return true; | |
| } | |
| } | |
| head.remove(last); | |
| return false; | |
| } | |
| private final ArrayList<Piece> test = new ArrayList<Piece>(); | |
| public boolean search(String word){ | |
| word = word.trim().toLowerCase(); | |
| char first = word.charAt(0); | |
| for(int r = 0; r < grid.length; r++){ | |
| for(int c = 0; c < grid[r].length; c++){ | |
| Piece p = this.grid[r][c]; | |
| if(p.chr == first) test.clear(); | |
| if(p.chr == first && this.search(test, word, p, 1)) return true; | |
| } | |
| } | |
| return false; | |
| } | |
| public static void main(String[] args) { | |
| WordSolver solver = new WordSolver(new char[][]{ | |
| {'A', 'M', 'A', 'C'}, | |
| {'L', 'T', 'I', 'I'}, | |
| {'L', 'N', 'A', 'D'}, | |
| {'Y', 'R', 'İ', 'Y'} | |
| }); | |
| Locale.setDefault(locale); | |
| int count = 0; | |
| long start = System.currentTimeMillis(); | |
| try { | |
| BufferedReader dict = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Yengas\\Desktop\\WordList\\turkishWords.lst"), "UTF-8")); | |
| String line = null; | |
| while((line = dict.readLine()) != null){ | |
| if(solver.search(line)){ System.out.println(line); count++; } | |
| } | |
| dict.close(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms"); | |
| System.out.println("Found: " + count); | |
| } | |
| } |
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
| a | |
| ac | |
| aci | |
| acima | |
| ad | |
| adi | |
| adim | |
| aidat | |
| ait | |
| al | |
| almac | |
| alt | |
| alti | |
| altin | |
| ama | |
| amac | |
| amin | |
| amit | |
| an | |
| ani | |
| anit | |
| anlam | |
| anlama | |
| anlatim | |
| anlatma | |
| ant | |
| ar | |
| ari | |
| at | |
| ata | |
| atama | |
| ati | |
| atici | |
| atim | |
| atlama | |
| ay | |
| aydin | |
| aydinlatici | |
| aydinlatma | |
| ayi | |
| ayin | |
| c | |
| cam | |
| cami | |
| camia | |
| cida | |
| cidar | |
| cim | |
| cima | |
| cin | |
| cinai | |
| d | |
| da | |
| daim | |
| daima | |
| dair | |
| dar | |
| dari | |
| dayi | |
| din | |
| dinar | |
| dini | |
| dir | |
| diyar | |
| i | |
| ic | |
| icat | |
| icim | |
| icin | |
| idari | |
| ima | |
| imal | |
| imla | |
| in | |
| inat | |
| inim | |
| intac | |
| iradi | |
| iran | |
| irat | |
| ita | |
| l | |
| la | |
| lam | |
| lama | |
| lata | |
| latin | |
| m | |
| mac | |
| mal | |
| malt | |
| malta | |
| mat | |
| matla | |
| mi | |
| miat | |
| midi | |
| midyat | |
| mini | |
| mit | |
| n | |
| na | |
| nadim | |
| nadir | |
| nar | |
| nida | |
| r | |
| rant | |
| ray | |
| rint | |
| riya | |
| t | |
| ta | |
| tac | |
| tadim | |
| tam | |
| tan | |
| tani | |
| tanim | |
| tanri | |
| tar | |
| tay | |
| tayin | |
| ti | |
| tim | |
| tin | |
| tini | |
| y | |
| ya | |
| yad | |
| yan | |
| yani | |
| yanit | |
| yar | |
| yari | |
| yarin | |
| yarinti | |
| yat | |
| yati | |
| yatma | |
| yir | |
| Took: 43ms | |
| Found: 141 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment