Skip to content

Instantly share code, notes, and snippets.

@cmh114933
Created June 10, 2019 02:20
Show Gist options
  • Save cmh114933/13d9a067492752c53715bed1e897980b to your computer and use it in GitHub Desktop.
Save cmh114933/13d9a067492752c53715bed1e897980b to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Arrays;
/**
* Sudoku
*/
public class Sudoku {
static void displayBoard(String boardString){
String[] boardRows = {"", "", "", "", "", "", "", "", ""};
char[] boardArray = boardString.toCharArray();
for(int index = 0; index < boardString.length(); index ++ ){
boardRows[index/9] = boardRows[index/9] + " " + boardArray[index];
if(index % 9 == 2 || index % 9 == 5){
boardRows[index/9] = boardRows[index/9] + " |";
}
}
System.out.println("-------------------------");
for(int index = 0; index < 9; index ++ ){
System.out.println("|" + boardRows[index] + " |");
if(index == 2 || index == 5){
System.out.println("-------------------------");
}
}
System.out.println("-------------------------");
}
static ArrayList<Character> findPossibleSolutions(String boardString, int index){
ArrayList<Character> possibleSolutions = new ArrayList<Character>(Arrays.asList('1', '2', '3', '4','5','6','7','8','9'));
char[] boardChars = boardString.toCharArray();
String[] usedNumbersInRow = {"", "", "", "", "", "", "", "", ""};
for(int i = 0; i < boardString.length(); i ++ ){
if(boardChars[i] != '_'){
usedNumbersInRow[i/9] = usedNumbersInRow[i/9] + boardChars[i];
}
}
char[] tempArray = usedNumbersInRow[index/9].toCharArray();
ArrayList<Character> rowEliminationValues = new ArrayList<Character>() ;
for(char c: tempArray){
rowEliminationValues.add(c);
}
possibleSolutions.removeAll(rowEliminationValues);
return possibleSolutions;
}
static String solve(String boardString){
displayBoard(boardString);
char[] boardChars = boardString.toCharArray();
for(int index = 0; index < boardChars.length; index ++ ){
if(boardChars[index] == '_'){
findPossibleSolutions(boardString, index);
}
}
return "";
}
static String guess(String boardString){
// Variables to keep track
// Pseudo Code
// For each cell
// If cell is empty
// find possible solutions for cell
// if there are possible solutions
// take the first solution in list and put into cell
// if there are no possible solutions
// backtrack
// find the previous emptyCell which I filled in
// revert to empty
// keep track of the wrong solution
// find possible solutions, exclude the wrong solution, and take the first solution, put into cell
// Iterations
// -----------
// Iteration 1: Cell at index 1:
// Guessed Value : 4
// Possible solutions: {4,6,7,9}
// Wrong solutions: {3}
// Iteration 2: Cell at index 4:
// Guessed Value :
// Possible solutions: {3,6,7,8}
// Wrong solutions: {}
// Iteration 3: Cell at index 6: {}
return "";
}
public static void main(String[] args) {
// Easy Mode
String solution = solve("1_58_2____9__764_52__4__819_19__73_6762_83_9_____61_5___76___3_43__2_5_16__3_89__");
System.out.println("Solution: " + solution);
System.out.println("Result: "
+ solution.equals("145892673893176425276435819519247386762583194384961752957614238438729561621358947"));
// // Hard Mode
// String solution =
// solve("400000805030000000000700000020000060000080400000010000000603070500200000104000000");
// System.out.println(solution.equals("417369825632158947958724316825437169791586432346912758289643571573291684164875293"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment