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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::cmp::max;
const NEIGHBOR: [(i8, i8); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)];
/// solves a binairo
///
/// holds the current state of the Board
/// and provides some methos to solve the binairo
pub struct Board {
width: usize,
max_0: usize,
max_1: usize,
field: Vec<Vec<i8>>,
possible: Vec<Vec<Vec<i8>>>,
}
impl Board {
/// create a new (empty) board
pub fn new(width: usize) -> Self {
Self {
width,
max_0: 0,
max_1: 0,
field: vec![vec![-1; width]; width],
possible: vec![vec![vec![0, 1]; width]; width],
}
}
/// initialyze the Board
///
/// based on the givven input String (created by the init - module)
pub fn init(&mut self, abc: &str) {
let mut mabc = abc.chars().rev().collect::<String>();
for y in 0..self.width {
for x in 0..self.width {
let val = max(47, mabc.pop().unwrap() as i8) - 48;
if -1 < val {
self.set_field(x, y, val);
}
}
}
}
/// solve the current binairo Board
///
/// and return solutions - vector, based of the requested number of solutions
pub fn solve(&mut self, num: usize) -> Vec<String> {
let mut sol: Vec<String> = vec![];
let mut obvi: bool = self.set_first_obvious();
if !self.is_valid() {
return sol;
}
while obvi {
obvi = self.set_first_obvious();
if !self.is_valid() {
return sol;
}
}
let (next_x, next_y) = self.get_first_guess();
if next_x == self.width || next_y == self.width {
if self.is_valid() {
sol.push(self.print());
}
return sol;
}
for next_val in &self.possible[next_y][next_x] {
let mut nxt_move: Board = Board {
width: self.width,
max_0: self.max_0,
max_1: self.max_1,
field: self.field.clone(),
possible: self.possible.clone(),
};
nxt_move.set_field(next_x, next_y, *next_val);
if nxt_move.is_valid() {
let mut nxt_sol = nxt_move.solve(num - sol.len());
if !nxt_sol.is_empty() {
sol.append(&mut nxt_sol);
if num <= sol.len() {
return sol;
}
}
}
}
sol
}
/// Set a cell to a given value (0 or 1)
///
/// and call set_pfeld ( in order to maintain the possible values for each cell )
fn set_field(&mut self, x: usize, y: usize, val: i8) {
self.field[y][x] = val;
self.set_pfeld(x, y, val);
}
/// update the possible values for each cell
///
/// based on the new value val in cell(x,y)
fn set_pfeld(&mut self, x: usize, y: usize, val: i8) {
self.possible[y][x] = vec![val];
for (nx, ny) in NEIGHBOR {
let y_tst: i8 = y as i8 + nx;
let x_tst: i8 = x as i8 + ny;
if valid_coord(&self.width, &x_tst, &y_tst) {
let neigh_val = self.field[y_tst.abs() as usize][x_tst.abs() as usize];
if neigh_val == val {
// println!(" Identical Neighbor found at {}, {}", x_tst, y_tst);
let y_two: i8 = y as i8 + 2 * nx;
let x_two: i8 = x as i8 + 2 * ny;
if valid_coord(&self.width, &x_two, &y_two) {
// println!(" Modify NeighborPoss found at {}, {}", x_two, y_two);
self.possible[y_two.abs() as usize][x_two.abs() as usize]
.retain(|e| e != &val);
}
let y_two: i8 = y as i8 - nx;
let x_two: i8 = x as i8 - ny;
if valid_coord(&self.width, &x_two, &y_two) {
// println!(" Modify NeighborPoss found at {}, {}", x_two, y_two);
self.possible[y_two.abs() as usize][x_two.abs() as usize]
.retain(|e| e != &val);
}
} else if neigh_val == -1 {
let y_two: i8 = y as i8 + 2 * nx;
let x_two: i8 = x as i8 + 2 * ny;
if valid_coord(&self.width, &x_two, &y_two)
&& self.field[y_two.abs() as usize][x_two.abs() as usize] == val
{
// println!(" Modify NeighborPoss found at {}, {}", x_tst, y_tst);
self.possible[y_tst.abs() as usize][x_tst.abs() as usize]
.retain(|e| e != &val);
}
}
}
}
let (row_0, col_0, row_1, col_1) = self.counter(x, y);
if self.max_0 + self.max_1 < self.width {
if self.max_0 < max(row_0, col_0) {
self.max_0 = max(row_0, col_0);
}
if self.max_1 < max(row_1, col_1) {
self.max_1 = max(row_1, col_1);
}
}
if self.max_0 + self.max_1 == self.width {
if row_0 == self.max_0 && row_1 < self.max_1 {
/*
let my_range = 0..self.width;
my_range
.into_iter()
.filter(|i| self.field[y][*i] == -1)
.for_each(|j| self.possible[y][j].retain(|e| *e == 1));
*/
for i in 0..self.width {
if self.field[y][i] == -1 {
self.possible[y][i].retain(|e| *e == 1);
}
}
}
if row_0 < self.max_0 && row_1 == self.max_1 {
for i in 0..self.width {
if self.field[y][i] == -1 {
self.possible[y][i].retain(|e| *e == 0);
}
}
}
if col_0 == self.max_0 && col_1 < self.max_1 {
for i in 0..self.width {
if self.field[i][x] == -1 {
self.possible[i][x].retain(|e| *e == 1);
}
}
}
if col_0 < self.max_0 && col_1 == self.max_1 {
for i in 0..self.width {
if self.field[i][x] == -1 {
self.possible[i][x].retain(|e| *e == 0);
}
}
}
}
}
/// counts the 0s and 1s in the row and column for a given cell(x, y)
fn counter(&self, x: usize, y: usize) -> (usize, usize, usize, usize) {
let mut row_0: usize = 0;
let mut col_0: usize = 0;
let mut row_1: usize = 0;
let mut col_1: usize = 0;
for i in 0..self.width {
match self.field[y][i] {
0 => row_0 += 1,
1 => row_1 += 1,
_ => (),
}
match self.field[i][x] {
0 => col_0 += 1,
1 => col_1 += 1,
_ => (),
}
}
(row_0, col_0, row_1, col_1)
}
/// check teh current state of the board
///
/// returns wether the current state is valid, or not
fn is_valid(&self) -> bool {
let mut max_0 = 0;
let mut max_1 = 0;
for y in 0..self.width {
for x in 0..self.width {
if self.possible[y][x].is_empty() {
return false;
}
}
let (row_0, col_0, row_1, col_1) = self.counter(y, y);
if (self.width + 1) / 2 < row_0
|| (self.width + 1) / 2 < row_1
|| (self.width + 1) / 2 < col_0
|| (self.width + 1) / 2 < col_1
{
return false;
}
if max_0 < max(row_0, col_0) {
max_0 = max(row_0, col_0)
}
if max_1 < max(row_1, col_1) {
max_1 = max(row_1, col_1)
}
if self.width < max_0 + max_1 {
return false;
}
}
true
}
/// provides a String representing the cell-values of the board
///
/// returns the cell-values of teh board as a String of length width * width
fn print(&self) -> String {
let mut abc: String = String::new();
for y in 0..self.width {
for x in 0..self.width {
abc.push(char::from((self.field[y][x] + 48) as u8));
}
}
abc
}
/// Sets the first obvious cell
///
/// Searches the first empty cell that can be solved, and sets a cell-value
/// returns, wether it did find such a cell, or not
fn set_first_obvious(&mut self) -> bool {
for y in 0..self.width {
for x in 0..self.width {
if self.field[y][x] == -1 && self.possible[y][x].len() == 1 {
self.set_field(x, y, self.possible[y][x][0]);
return true;
}
}
}
false
}
fn get_first_guess(&self) -> (usize, usize) {
for y in 0..self.width {
for x in 0..self.width {
if self.possible[y][x].len() == 2 {
return (x, y);
}
}
}
(self.width, self.width)
}
}
/// verifies coordinates
///
/// internal function, returns
/// - wether tuple (x, y) does fir into a board with dimension width x width
fn valid_coord(width: &usize, x: &i8, y: &i8) -> bool {
0 <= *x && *x < *width as i8 && 0 <= *y && *y < *width as i8
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_4x4() {
let mut test_board = Board {
width: 4,
max_0: 0,
max_1: 0,
field: vec![vec![-1; 4]; 4],
possible: vec![vec![vec![0, 1]; 4]; 4],
};
test_board.init(".1.0..0..0..11.0");
let solu = test_board.solve(1);
assert_eq!(vec![String::from("0110100100111100")], solu);
}
#[test]
fn test_number_of_solutions() {
let sol_num: usize = 3;
let mut test_board = Board {
width: 10,
max_0: 0,
max_1: 0,
field: vec![vec![-1; 10]; 10],
possible: vec![vec![vec![0, 1]; 10]; 10],
};
test_board.init("......10...0..........1..1.1...010.0...1.0....0.1.....0..11...0...........01....11.0.1....1...1....0");
let solu = test_board.solve(10);
assert_eq!(sol_num, solu.len());
let solu = test_board.solve(3);
assert_eq!(sol_num, solu.len());
let solu = test_board.solve(2);
assert_eq!(2, solu.len());
let solu = test_board.solve(1);
assert_eq!(1, solu.len());
}
}