From 3e79e357e710b05053ae5ceae6a1a11eee6261c1 Mon Sep 17 00:00:00 2001 From: f4r4z Date: Wed, 14 Mar 2018 22:48:41 -0400 Subject: [PATCH] initialize --- Sudoku.java | 114 ++++++++++++++++++++++++++++++++++++++++++++++ SudokuClient.java | 89 ++++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 Sudoku.java create mode 100644 SudokuClient.java diff --git a/Sudoku.java b/Sudoku.java new file mode 100644 index 0000000..ef65a22 --- /dev/null +++ b/Sudoku.java @@ -0,0 +1,114 @@ +public class Sudoku { + + private int[][] board; + + //constructor + public Sudoku (int[][] board) { + this.board = board; + } + + //get board + public int[][] getBoard() { + return board; + } + + + //solve method helper + public void solve() { + solve(0,0); + } + + //solves the next position and returns true if it is possible + private boolean nextPos(int row, int col) { + + if (col < 8) { + return solve(row, col + 1); + } else { + return solve(row + 1, 0); + } + } + + //solves the sudoku + private boolean solve(int row, int col) { + + //base case + if(row > 8) { + return true; + } + + if(board[row][col] != 0) { + if(check(row, col, board[row][col])) { + return nextPos(row, col); + } + } + + + int num = board[row][col]; + + //checks each number from 1 to 9 + for(int i = 1; i < 10; i++) { + + if(check(row, col, i)) { + + board[row][col] = i; + if(nextPos(row, col)) { + return true; + } + + + + } + + //clears the previous positions + board[row][col] = 0; + + } + + + return false; + } + + //checks for each scenario + public boolean check(int row, int col, int x) { + + //checks for rows and column + for(int i = 0; i < board.length; i++) { + if(i != row && board[i][col] == x) { + return false; + } + + if(i != col && board[row][i] == x) { + return false; + } + } + + + int squareRow = (row/3) * 3; + int squareCol = (col/3) * 3; + + + //checks for 3x3 + for(int i = squareRow; i < squareRow+3; i++) { + for(int j = squareCol; j < squareCol+3; j++) { + if((j != col || i != row) && board[i][j] == x) { + return false; + } + } + } + + return true; + } + + + + //prints the board + public void print() { + for(int r = 0; r < board.length; r++) { + for (int c = 0; c < board[r].length; c++) + System.out.print(board[r][c] + " "); + System.out.println(); + } + } + + +} diff --git a/SudokuClient.java b/SudokuClient.java new file mode 100644 index 0000000..77365f3 --- /dev/null +++ b/SudokuClient.java @@ -0,0 +1,89 @@ + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Scanner; + +public class SudokuClient { + public static void main(String[] args) throws MalformedURLException { + + + + //World's hardest sudoku + int[][] board = { + {8,0,0,0,0,0,0,0,0}, + {0,0,3,6,0,0,0,0,0}, + {0,7,0,0,9,0,2,0,0}, + {0,5,0,0,0,7,0,0,0}, + {0,0,0,0,4,5,7,0,0}, + {0,0,0,1,0,0,0,3,0}, + {0,0,1,0,0,0,0,6,8}, + {0,0,8,5,0,0,0,1,0}, + {0,9,0,0,0,0,4,0,0}, + }; + + Sudoku test = new Sudoku(board); + + test.solve(); + test.print(); + + + //Project Euler Problem 96 + URL file = new URL("https://projecteuler.net/project/resources/p096_sudoku.txt"); + int sum = 0; + + //whole text file + String s = ""; + + try { + Scanner scanner = new Scanner(file.openStream()); + + + while(scanner.hasNextLine()) { + + s += scanner.nextLine() + "\n"; + + + } + + } catch (IOException e) { + System.out.println(e); + } + + //Array of lines of the text + String[] lines = s.split(System.getProperty("line.separator")); + + //Goes over each line + for(int i = 0; i < lines.length; i++) { + + int[][] sudoku96 = new int[9][9]; + + //If line is not a title, stores it in a 2D array + if(lines[i].contains("Grid")) { + for(int r = 0; r < sudoku96.length; r++) { + for(int c = 0; c < sudoku96[r].length; c++) { + + sudoku96[r][c] = Character.getNumericValue(lines[i+ r + 1].charAt(c)); + + + } + } + + //Sudoku class + Sudoku sudokuSolver = new Sudoku(sudoku96); + sudokuSolver.solve(); + sudoku96 = sudokuSolver.getBoard(); + String digits = sudoku96[0][0] + "" + sudoku96[0][1] + "" + sudoku96[0][2]; + sum += Integer.parseInt(digits); + + } + + + + } + + System.out.println(); + System.out.println(sum); + + } +}