-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuExample.java
80 lines (59 loc) · 2.19 KB
/
SudokuExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class SudokuExample {
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();
//adds the 3 top left digits
String digits = sudoku96[0][0] + "" + sudoku96[0][1] + "" + sudoku96[0][2];
sum += Integer.parseInt(digits);
}
}
System.out.println();
System.out.println(sum);
}
}