-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem96.java
214 lines (175 loc) · 5.92 KB
/
problem96.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Javagony :)
import java.util.*;
import java.io.*;
public class problem96 {
public static final int[] BOX_OFFSETS = new int[] { 0, 1, 2, 9, 10, 11, 18, 19, 20 };
private int totalSum;
public problem96() {
totalSum = 0;
}
// These are our if statements. They don't look much like if
// statements, but hey.
public class HaltProgramException extends RuntimeException {}
public class PuzzleLoadedException extends RuntimeException {}
public class LineLoadedException extends RuntimeException {}
public class SuccessfullySolvedException extends RuntimeException {}
public class GivenCellException extends RuntimeException {}
public class EndFindValidException extends RuntimeException {}
public class MustBacktrackException extends RuntimeException {}
public class NotValidOptionException extends RuntimeException {}
public class InvalidateOptionException extends RuntimeException {}
public static void noop(int a) {
// Need this to get around Java's statement/expression issues.
}
public static void throwIfTrue(boolean condition, RuntimeException exception) {
try {
noop(1 / Boolean.compare(condition, true));
} catch (ArithmeticException ignore) {
throw exception;
}
}
public class ValidOptionsFinder {
private int[] puzzle;
private int idx;
private boolean[] valid;
public ValidOptionsFinder(int[] puzzle, int idx) {
this.puzzle = puzzle;
this.idx = idx;
this.valid = new boolean[9];
Arrays.fill(valid, true);
}
public boolean[] getValidOptions() {
return valid;
}
private void _rowResolution(int col) {
throwIfTrue(col >= 9, new EndFindValidException());
int row = idx / 9;
try {
throwIfTrue(puzzle[row * 9 + col] > 0, new InvalidateOptionException());
} catch (InvalidateOptionException e) {
valid[ puzzle[row * 9 + col] - 1 ] = false;
}
_rowResolution(col + 1);
}
private void _colResolution(int row) {
throwIfTrue(row >= 9, new EndFindValidException());
int col = idx % 9;
try {
throwIfTrue(puzzle[row * 9 + col] > 0, new InvalidateOptionException());
} catch (InvalidateOptionException e) {
valid[ puzzle[row * 9 + col] - 1 ] = false;
}
_colResolution(row + 1);
}
private int getBox() {
int rowbox = idx / 27;
int colbox = (idx % 9) / 3;
return rowbox * 27 + colbox * 3;
}
private void _boxResolution(int innerIdx) {
throwIfTrue(innerIdx >= 9, new EndFindValidException());
int box = getBox();
int curr = box + BOX_OFFSETS[innerIdx];
try {
throwIfTrue(puzzle[curr] > 0, new InvalidateOptionException());
} catch (InvalidateOptionException e) {
valid[ puzzle[curr] - 1 ] = false;
}
_boxResolution(innerIdx + 1);
}
public void findOption() {
try {
_rowResolution(0);
} catch (EndFindValidException ignore) {}
try {
_colResolution(0);
} catch (EndFindValidException ignore) {}
try {
_boxResolution(0);
} catch (EndFindValidException ignore) {}
}
}
public class SinglePuzzleSolver {
private int[] puzzle;
public SinglePuzzleSolver() {
puzzle = new int[81];
}
public int[] getPuzzle() {
return puzzle;
}
private void _loadLine(int i, int j, String x) {
throwIfTrue(j == 9, new LineLoadedException());
puzzle[i * 9 + j] = (int)(x.charAt(j) - '0');
_loadLine(i, j + 1, x);
}
private void _loadPuzzle(int i, BufferedReader reader) throws IOException {
throwIfTrue(i == 9, new PuzzleLoadedException());
String line = reader.readLine();
try {
_loadLine(i, 0, line);
} catch (LineLoadedException e) {}
_loadPuzzle(i + 1, reader);
}
public void loadPuzzle(BufferedReader reader) throws IOException {
try {
reader.readLine(); // Ignore "GRID XX" line
_loadPuzzle(0, reader);
} catch (PuzzleLoadedException ignore) {}
}
private void _trySolutions(int idx, boolean[] valid, int validIdx) {
try {
throwIfTrue(validIdx >= 9, new MustBacktrackException());
} catch (MustBacktrackException e) {
puzzle[idx] = 0;
throw e;
}
try {
throwIfTrue(!valid[validIdx], new NotValidOptionException());
} catch (NotValidOptionException ignore) {
_trySolutions(idx, valid, validIdx + 1);
}
puzzle[idx] = validIdx + 1;
try {
_solvePuzzle(idx + 1);
} catch (MustBacktrackException ignore) {
_trySolutions(idx, valid, validIdx + 1);
}
}
private void _solvePuzzle(int idx) {
throwIfTrue(idx >= 81, new SuccessfullySolvedException());
try {
throwIfTrue(puzzle[idx] > 0, new GivenCellException());
} catch (GivenCellException ignore) {
_solvePuzzle(idx + 1);
}
ValidOptionsFinder finder = new ValidOptionsFinder(puzzle, idx);
finder.findOption();
boolean[] valid = finder.getValidOptions();
_trySolutions(idx, valid, 0);
}
public void solvePuzzle() {
try {
_solvePuzzle(0);
} catch (SuccessfullySolvedException ignore) {}
}
}
public int getTotalSum() {
return totalSum;
}
public void solveAllPuzzles(int i, BufferedReader reader) throws IOException {
throwIfTrue(i == 0, new HaltProgramException());
SinglePuzzleSolver solver = new SinglePuzzleSolver();
solver.loadPuzzle(reader);
solver.solvePuzzle();
int[] puzzle = solver.getPuzzle();
totalSum += puzzle[0] * 100 + puzzle[1] * 10 + puzzle[2];
solveAllPuzzles(i - 1, reader);
}
public static void main(String[] args) {
problem96 solver = new problem96();
try (BufferedReader reader = new BufferedReader(new FileReader("./files/p096_sudoku.txt"))) {
solver.solveAllPuzzles(50, reader);
} catch (Exception e) {}
System.out.println(solver.getTotalSum());
}
}