Solve 96 using my Sudoku solver from college

main
Felix Martin 2021-04-22 16:26:20 -04:00
parent 7825acbd73
commit 57286cdaf1
3 changed files with 73 additions and 15 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.swp
*.class
.ipynb_checkpoints
__pycache__
euler.sublime-workspace

38
other/e096.java Normal file
View File

@ -0,0 +1,38 @@
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int N_SUDOKUS = 50;
Sudoku[] sudokus = new Sudoku[N_SUDOKUS];
try {
File f = new File("../txt/e096.txt");
Scanner s = new Scanner(f);
for (int i = 0; i < N_SUDOKUS; i++) {
sudokus[i] = new Sudoku();
sudokus[i].loadFieldFromScanner(s);
}
s.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
int s = 0;
for (int i = 0; i < N_SUDOKUS; i++) {
// sudokus[i].printField();
sudokus[i].solveSudoku();
s += sudokus[i].getThreeDigits();
// sudokus[i].printField();
}
System.out.println("e096: " + s);
}
}

View File

@ -34,6 +34,40 @@ class Sudoku {
System.out.println(fieldOut);
}
int getThreeDigits() {
int i = 0;
i += fieldValues[2];
i += fieldValues[1] * 10;
i += fieldValues[0] * 100;
return i;
}
void loadFieldFromScanner(Scanner s) throws Exception {
int currentField = 0;
if (!s.hasNextLine()) {
throw new Exception("No lines to read");
}
if (!s.nextLine().startsWith("Grid")) {
throw new Exception("Not at start of grid");
}
while (s.hasNextLine()) {
String line = s.nextLine();
for (int i = 0; i < line.length(); i++) {
int v = line.charAt(i) - (int) '0';
fieldValues[currentField] = v;
currentField += 1;
}
if (currentField == 81) {
break;
}
}
return;
}
/** Function asks for an file name which
* should contain a sudoku and this sudoku
* into fieldValues, Empty fields should
@ -131,20 +165,5 @@ class Sudoku {
return false;
}
}
/** This class is to test the
* Sudoku class and it's functions
*/
class SudokuDemo {
public static void main(String[] args) {
System.out.println("Sudoku demo loading...");
Sudoku s = new Sudoku();
s.loadFieldFromFile();
s.printField();
//s.testAllowed();
s.solveSudoku();
s.printField();
}
}