-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rs
768 lines (663 loc) · 26 KB
/
board.rs
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use std::fmt;
use std::ops::{BitAnd, BitAndAssign};
use crate::io::format_for_fancy_console;
use crate::layout::{Cell, CellSet, House, Known, KnownSet, Value};
use crate::solve::creates_deadly_rectangles;
use super::{Effects, Error, PseudoCell, Strategy};
/// Indicates the result of setting a given or known or removing a candidate.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Change {
None,
Valid,
Invalid,
}
impl Change {
pub fn changed(self) -> bool {
self != Change::None
}
pub fn and(self, other: Change) -> Change {
match (self, other) {
(Change::None, _) => other,
(_, Change::None) => self,
(Change::Valid, Change::Valid) => Change::Valid,
_ => Change::Invalid,
}
}
}
impl BitAnd for Change {
type Output = Change;
fn bitand(self, rhs: Self) -> Self::Output {
self.and(rhs)
}
}
impl BitAndAssign for Change {
fn bitand_assign(&mut self, rhs: Self) {
*self = self.and(rhs);
}
}
/// Tracks the full state of a puzzle in play.
///
/// To allow solvers to run quickly, the state of the board
/// is stored in several forms, duplicating data to provide
/// performant read access at the cost of slower writes
/// and increased memory consumption.
///
/// The givens are cells that begin with a digit, the clues given
/// by the puzzle creator to make it solvable. When a cell
/// is either given as a clue or been solved with a digit,
/// it is called known.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Board {
/// Cells that were given a digit at the start,
/// often referred to as clues.
givens: CellSet,
/// All cells with a digit, both givens and solved cells.
knowns: CellSet,
/// Value of each cell, either a digit or unknown.
values: [Value; 81],
/// Set of available digits that may still be set for each cell.
candidate_knowns_by_cell: [KnownSet; 81],
/// Set of available cells for each digit.
candidate_cells_by_known: [CellSet; 9],
/// Every cell that has N candidates.
cells_with_n_candidates: [CellSet; 10],
/// Every cell solved or given for each digit.
solved_cells_by_known: [CellSet; 9],
}
impl Board {
/// Creates a new board with no givens and all cells unsolved.
#[rustfmt::skip]
pub const fn new() -> Board {
Board {
givens: CellSet::empty(),
knowns: CellSet::empty(),
values: [Value::unknown(); 81],
candidate_knowns_by_cell: [KnownSet::full(); 81],
candidate_cells_by_known: [CellSet::full(); 9],
cells_with_n_candidates: [
CellSet::empty(), CellSet::empty(), CellSet::empty(),
CellSet::empty(), CellSet::empty(), CellSet::empty(),
CellSet::empty(), CellSet::empty(), CellSet::empty(),
CellSet::full(),
],
solved_cells_by_known: [CellSet::empty(); 9],
}
}
/// Returns true if the cell is unknown.
pub const fn is_unknown(&self, cell: Cell) -> bool {
!self.knowns.has(cell)
}
/// Returns the number of unknown cells in the puzzle.
pub const fn unknown_count(&self) -> usize {
81 - self.knowns.len()
}
/// Returns the set of all unknown cells.
pub fn unknowns(&self) -> CellSet {
!self.knowns
}
/// Returns an iterator of all unknown cells with their candidates.
pub fn unknown_iter(&self) -> impl Iterator<Item = (Cell, KnownSet)> + '_ {
self.unknowns()
.into_iter()
.map(|cell| (cell, self.candidates(cell)))
}
/// Returns true if the cell is known or a given.
pub const fn is_known(&self, cell: Cell) -> bool {
self.knowns.has(cell)
}
/// Returns the number of known cells in the puzzle, including givens.
pub const fn known_count(&self) -> usize {
self.knowns.len()
}
/// Returns the set of all known cells, including givens.
pub const fn knowns(&self) -> CellSet {
self.knowns
}
/// Returns the set of all cells solved with the known, including givens.
pub const fn known_cells(&self, known: Known) -> CellSet {
self.solved_cells_by_known[known.usize()]
}
/// Returns an iterator of all known cells with their digit, including givens.
pub fn known_iter(&self) -> impl Iterator<Item = (Cell, Known)> + '_ {
self.knowns
.into_iter()
.map(|cell| (cell, self.value(cell).known().unwrap()))
}
/// Returns an iterator of the cells which are known with their digit, including givens.
pub fn knowns_iter(&self, cells: CellSet) -> impl Iterator<Item = (Cell, Known)> + '_ {
(cells & self.knowns)
.into_iter()
.map(|cell| (cell, self.value(cell).known().unwrap()))
}
/// Returns the set of digits to which any of the cells is set.
pub fn all_knowns(&self, cells: CellSet) -> KnownSet {
cells.iter().fold(KnownSet::empty(), |acc, cell| {
self.value(cell).known().map_or(acc, |k| acc + k)
})
}
/// Returns true if a cell in the house has the digit.
pub fn is_house_known(&self, house: House, known: Known) -> bool {
!(self.solved_cells_by_known[known.usize()] & house.cells()).is_empty()
}
/// Returns true if the cell is a given.
pub const fn is_given(&self, cell: Cell) -> bool {
self.givens.has(cell)
}
/// Returns the number of givens in the puzzle.
pub const fn given_count(&self) -> usize {
self.givens.len()
}
/// Returns the set of all givens.
pub const fn givens(&self) -> CellSet {
self.givens
}
/// Returns the set of all cells given the known.
pub fn given_cells(&self, known: Known) -> CellSet {
self.givens & self.solved_cells_by_known[known.usize()]
}
/// Returns true if the cell could not have been solved by the known due to a peer with the given.
pub fn blocked_by_given(&self, cell: Cell, known: Known) -> bool {
!(cell.peers() & self.givens & self.solved_cells_by_known[known.usize()]).is_empty()
}
/// Returns true if every cell on the board has a digit.
pub const fn is_fully_solved(&self) -> bool {
self.knowns.is_full()
}
/// Returns true if the cell is solved but not given.
pub const fn is_solved(&self, cell: Cell) -> bool {
self.knowns.has(cell) && !self.givens.has(cell)
}
/// Returns the number of solved cells in the puzzle, excluding givens.
pub const fn solved_count(&self) -> usize {
self.knowns.len() - self.givens.len()
}
/// Returns the set of all solved cells, excluding givens.
pub fn solved(&self) -> CellSet {
self.knowns - self.givens
}
/// Returns the set of all cells solved with the known, excluding givens.
pub fn solved_cells(&self, known: Known) -> CellSet {
self.solved_cells_by_known[known.usize()] - self.givens
}
/// Returns true if every cell in the house has a digit.
pub fn is_house_solved(&self, house: House) -> bool {
(!self.knowns & house.cells()).is_empty()
}
/// Returns the value of the cell, either a digit or unknown.
pub const fn value(&self, cell: Cell) -> Value {
self.values[cell.usize()]
}
/// Sets the cell to the candidate, marks it as a given,
/// and returns true along with any follow-up actions found.
///
/// See [`Board::set_known()`] for more details.
pub fn set_given(&mut self, cell: Cell, known: Known, effects: &mut Effects) -> Change {
let change = self.set_known(cell, known, effects);
if change.changed() {
self.givens += cell;
}
change
}
/// Sets the cell to the candidate and returns true
/// along with any follow-up actions found.
///
/// The candidate is removed from the cell's peers
/// and its three houses, and the cell is removed
/// as a candidate for all of its other candidates
/// in its three houses.
///
/// If any errors are caused while setting the cell,
/// they are returned with the actions, and the puzzle
/// will be left in an unsolvable state, but the internal
/// state will be consistent.
///
/// Returns false with no actions or errors
/// if the known is not a candidate for the cell.
pub fn set_known(&mut self, cell: Cell, known: Known, effects: &mut Effects) -> Change {
if let Some(current) = self.value(cell).known() {
if current == known {
return Change::None;
} else {
effects.add_error(Error::AlreadySolved(cell, known, current));
return Change::Invalid;
}
} else if !self.is_candidate(cell, known) {
effects.add_error(Error::NotCandidate(cell, known));
return Change::Invalid;
}
if let Some(rectangles) = creates_deadly_rectangles(self, cell, known) {
rectangles.into_iter().for_each(|r| {
effects.add_error(Error::DeadlyRectangle(r));
});
}
self.values[cell.usize()] = known.value();
self.knowns += cell;
self.solved_cells_by_known[known.usize()] += cell;
self.candidate_cells_by_known[known.usize()] -= cell;
let mut change = Change::Valid;
let mut candidates = self.candidate_knowns_by_cell[cell.usize()];
self.cells_with_n_candidates[candidates.len()] -= cell;
self.cells_with_n_candidates[0] += cell;
candidates -= known;
self.candidate_knowns_by_cell[cell.usize()] = KnownSet::empty();
for known in candidates {
self.candidate_cells_by_known[known.usize()] -= cell;
change &= self.remove_candidate_cell_from_houses(cell, known, effects);
}
for peer in self.candidate_cells_by_known[known.usize()] & cell.peers() {
change &= self.remove_candidate(peer, known, effects);
// effects.add_erase(Strategy::Peer, peer, known)
}
change
}
/// Returns a new pseudo cell with the given cells and their candidates.
pub fn pseudo_cell(&self, cells: CellSet) -> PseudoCell {
PseudoCell::new(cells, self.all_candidates(cells))
}
/// Returns true if the cell has the candidate.
pub const fn is_candidate(&self, cell: Cell, known: Known) -> bool {
self.candidate_knowns_by_cell[cell.usize()].has(known)
}
/// Returns the set of candidates for the cell.
pub const fn candidates(&self, cell: Cell) -> KnownSet {
self.candidate_knowns_by_cell[cell.usize()]
}
/// Returns the set of combined candidates for the cells.
pub fn all_candidates(&self, cells: CellSet) -> KnownSet {
cells
.iter()
.fold(KnownSet::empty(), |acc, cell| acc | self.candidates(cell))
}
/// Returns the set of common candidates for the cells.
pub fn common_candidates(&self, cells: CellSet) -> KnownSet {
if cells.is_empty() {
return KnownSet::empty();
}
cells
.iter()
.fold(KnownSet::full(), |acc, cell| acc & self.candidates(cell))
}
/// Returns all cells that have N candidates.
pub const fn cells_with_n_candidates(&self, n: usize) -> CellSet {
debug_assert!(n <= 9);
self.cells_with_n_candidates[n]
}
/// Returns an iterator of unknown cells with N candidates with their candidates.
pub fn cell_candidates_with_n_candidates(
&self,
n: usize,
) -> impl Iterator<Item = (Cell, KnownSet)> + '_ {
self.cells_with_n_candidates(n)
.iter()
.map(|cell| (cell, self.candidates(cell)))
}
/// Returns the set of cells that have the candidate.
pub const fn candidate_cells(&self, known: Known) -> CellSet {
self.candidate_cells_by_known[known.usize()]
}
/// Returns the set of cells in the house that have the candidate.
pub fn house_candidate_cells(&self, house: House, known: Known) -> CellSet {
house.cells() & self.candidate_cells(known)
}
/// Removes the candidate from the cell and returns true
/// along with any follow-up actions found.
///
/// The cell is removed as a candidate from its three houses.
///
/// If any errors are caused while removing the candidate,
/// they are returned with the actions, and the puzzle
/// will be left in an unsolvable state, but the internal
/// state will be consistent.
///
/// Returns false with no actions or errors
/// if the known is not a candidate for the cell.
pub fn remove_candidate(&mut self, cell: Cell, known: Known, effects: &mut Effects) -> Change {
let knowns = &mut self.candidate_knowns_by_cell[cell.usize()];
if !knowns[known] {
return Change::None;
}
let size = knowns.len();
*knowns -= known;
self.cells_with_n_candidates[size] -= cell;
self.cells_with_n_candidates[size - 1] += cell;
self.candidate_cells_by_known[known.usize()] -= cell;
let mut change = Change::Valid;
if knowns.is_empty() {
effects.add_error(Error::UnsolvableCell(cell));
change = Change::Invalid;
} else if let Some(single) = knowns.as_single() {
effects.add_set(Strategy::NakedSingle, cell, single);
}
change & self.remove_candidate_cell_from_houses(cell, known, effects)
}
/// Removes the cell as a candidate for the known
/// from its three houses and returns true
/// along with any follow-up actions found.
///
/// If any errors are caused while removing the candidate,
/// they are returned with the actions, and the puzzle
/// will be left in an unsolvable state, but the internal
/// state will be consistent.
fn remove_candidate_cell_from_houses(
&mut self,
cell: Cell,
known: Known,
effects: &mut Effects,
) -> Change {
let mut change = Change::None;
for house in cell.houses() {
if self.is_house_known(house, known) {
continue;
}
change &= Change::Valid;
let candidates = self.house_candidate_cells(house, known);
if candidates.is_empty() {
effects.add_error(Error::UnsolvableHouse(house, known));
change &= Change::Invalid;
} else if candidates.len() == 1 {
let single = candidates.as_single().unwrap();
effects.add_set(Strategy::HiddenSingle, single, known);
}
}
change
}
/// Removes the candidates from the cell and returns true
/// along with any follow-up actions found.
///
/// See [`Board::remove_candidate()`] for more details.
pub fn remove_candidates(
&mut self,
cell: Cell,
knowns: KnownSet,
effects: &mut Effects,
) -> Change {
knowns.iter().fold(Change::None, |change, known| {
change & self.remove_candidate(cell, known, effects)
})
}
/// Removes the candidate from the cells and returns true
/// along with any follow-up actions found.
///
/// See [`Board::remove_candidate()`] for more details.
pub fn remove_candidate_from_cells(
&mut self,
cells: CellSet,
known: Known,
effects: &mut Effects,
) -> Change {
cells.iter().fold(Change::None, |change, cell| {
change & self.remove_candidate(cell, known, effects)
})
}
/// Removes the candidates from the cells and returns true
/// along with any follow-up actions found.
///
/// See [`Board::remove_candidate()`] for more details.
pub fn remove_candidates_from_cells(
&mut self,
cells: CellSet,
knowns: KnownSet,
effects: &mut Effects,
) -> Change {
cells.iter().fold(Change::None, |change, cell| {
change
& knowns.iter().fold(Change::None, |change, known| {
change & self.remove_candidate(cell, known, effects)
})
})
}
/// Returns a new board with the digits of this board
/// set as givens for the specified cells.
///
/// If any specified cell is not known in this board,
/// it is left unknown in the returned board.
pub fn with_givens(&self, pattern: CellSet) -> (Board, Effects) {
(pattern & self.knowns()).iter().fold(
(Board::new(), Effects::new()),
|(mut b, mut e), c| {
b.set_given(c, self.value(c).known().unwrap(), &mut e);
(b, e)
},
)
}
/// Returns a new board with the digits of this board
/// except for the one in the given cell.
pub fn without(&self, cell: Cell) -> (Board, Effects) {
self.known_iter().filter(|(c, _)| *c != cell).fold(
(Board::new(), Effects::new()),
|(mut b, mut e), (c, k)| {
b.set_given(c, k, &mut e);
(b, e)
},
)
}
/// Returns the packed string format of the digits of this board
/// with a period for each unknown cell and no spacing between rows.
pub fn packed_string(&self) -> String {
let mut result = String::new();
House::rows_iter().for_each(|row| {
row.cells().iter().for_each(|cell| {
let value = self.value(cell);
if !value {
result.push('.');
} else {
result.push(value.label());
}
})
});
result
}
}
impl fmt::Display for Board {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format_for_fancy_console(self))
}
}
#[cfg(test)]
mod test {
use itertools::Itertools;
use crate::io::{Parse, Parser};
use crate::layout::cells::cell::cell;
use crate::layout::values::known::known;
use crate::testing::strip_leading_whitespace;
use super::*;
fn fixture() -> Board {
Parse::grid().parse_simple(
strip_leading_whitespace(
"
+-----------------+--------------------+-----------------+
| 48 59 2 | 1459 18 1589 | 3 7 6 |
| 478 1 468 | 24679 3 2689 | 5 248 248 |
| 3478 3567 4568 | 124567 12678 12568 | 148 9 248 |
+-----------------+--------------------+-----------------+
| 9 367 46 | 8 5 26 | 467 24 1 |
| 78 567 1568 | 3 126 4 | 6789 258 25789 |
| 2 56 14568 | 16 9 7 | 468 458 3 |
+-----------------+--------------------+-----------------+
| 6 8 9 | 1257 127 1235 | 147 1345 457 |
| 5 2 3 | 179 4 189 | 1789 6 789 |
| 1 4 7 | 569 68 35689 | 2 358 589 |
+-----------------+--------------------+-----------------+
",
)
.as_str(),
)
}
#[test]
fn test_new() {
let f = Board::new();
assert_eq!(f.unknown_count(), 81);
assert_eq!(f.unknowns(), CellSet::full());
assert_eq!(f.known_count(), 0);
assert_eq!(f.knowns(), CellSet::empty());
assert_eq!(f.all_knowns(CellSet::full()), KnownSet::empty());
assert_eq!(f.given_count(), 0);
assert_eq!(f.givens(), CellSet::empty());
assert_eq!(f.is_fully_solved(), false);
assert_eq!(f.solved_count(), 0);
assert_eq!(f.solved(), CellSet::empty());
for cell in Cell::iter() {
assert_eq!(f.is_unknown(cell), true);
assert_eq!(f.is_known(cell), false);
assert_eq!(f.is_given(cell), false);
assert_eq!(f.is_solved(cell), false);
assert_eq!(f.value(cell), Value::unknown());
assert_eq!(f.candidates(cell), KnownSet::full());
}
for known in Known::iter() {
assert_eq!(f.candidate_cells(known), CellSet::full());
}
for house in House::iter() {
assert_eq!(f.is_house_solved(house), false);
for known in Known::iter() {
assert_eq!(f.is_house_known(house, known), false);
}
}
}
#[test]
fn test_parsed() {
let f = fixture();
let solved = CellSet::from(
"A3 A7 A8 A9 B2 B5 B7 C8 D1 D4 D5 D9 E4 E6 F1 F5 F6 F9 G1 G2 G3 H1 H2 H3 H5 H8 J1 J2 J3 J7",
);
assert_eq!(f.unknown_count(), 81 - solved.len());
assert_eq!(f.unknowns(), CellSet::full() - solved);
assert_eq!(f.known_count(), solved.len());
assert_eq!(f.knowns(), solved);
assert_eq!(f.all_knowns(CellSet::full()), KnownSet::full());
assert_eq!(f.given_count(), 0);
assert_eq!(f.givens(), CellSet::empty());
assert_eq!(f.is_fully_solved(), false);
assert_eq!(f.solved_count(), solved.len());
assert_eq!(f.solved(), solved);
for cell in solved {
assert_eq!(f.is_unknown(cell), false);
assert_eq!(f.is_known(cell), true);
assert_eq!(f.is_given(cell), false);
assert_eq!(f.is_solved(cell), true);
assert_eq!(f.value(cell).is_known(), true);
assert_eq!(f.candidates(cell), KnownSet::empty());
}
}
#[test]
fn test_is_candidate() {
let f = fixture();
assert_eq!(f.is_candidate(cell!("A1"), known!("4")), true);
assert_eq!(f.is_candidate(cell!("A1"), known!("8")), true);
assert_eq!(f.is_candidate(cell!("C3"), known!("4")), true);
assert_eq!(f.is_candidate(cell!("C3"), known!("5")), true);
assert_eq!(f.is_candidate(cell!("C3"), known!("6")), true);
assert_eq!(f.is_candidate(cell!("C3"), known!("8")), true);
assert_eq!(f.is_candidate(cell!("A1"), known!("1")), false);
assert_eq!(f.is_candidate(cell!("A1"), known!("2")), false);
assert_eq!(f.is_candidate(cell!("A1"), known!("3")), false);
assert_eq!(f.is_candidate(cell!("A1"), known!("5")), false);
assert_eq!(f.is_candidate(cell!("A1"), known!("6")), false);
assert_eq!(f.is_candidate(cell!("A1"), known!("7")), false);
assert_eq!(f.is_candidate(cell!("A1"), known!("9")), false);
assert_eq!(f.is_candidate(cell!("H1"), known!("5")), false);
}
#[test]
fn test_candidates() {
let f = fixture();
assert_eq!(f.candidates(cell!("A1")), KnownSet::from("4 8"));
assert_eq!(f.candidates(cell!("C3")), KnownSet::from("4 5 6 8"));
assert_eq!(f.candidates(cell!("D1")), KnownSet::empty());
}
#[test]
fn test_all_candidates() {
let f = fixture();
assert_eq!(f.all_candidates(CellSet::empty()), KnownSet::empty());
assert_eq!(f.all_candidates(CellSet::full()), KnownSet::full());
assert_eq!(
f.all_candidates(CellSet::from("A1 A2")),
KnownSet::from("4 5 8 9")
);
assert_eq!(
f.all_candidates(CellSet::from("A1 A2 A3 A4")),
KnownSet::from("1 4 5 8 9")
);
}
#[test]
fn test_common_candidates() {
let f = fixture();
assert_eq!(f.common_candidates(CellSet::empty()), KnownSet::empty());
assert_eq!(f.common_candidates(CellSet::full()), KnownSet::empty());
assert_eq!(
f.common_candidates(CellSet::from("A2 A4")),
KnownSet::from("5 9")
);
assert_eq!(
f.common_candidates(CellSet::from("A1 A2 A3 A4")),
KnownSet::empty()
);
}
#[test]
fn test_cells_with_n_candidates() {
let f = fixture();
assert_eq!(f.cells_with_n_candidates(0), f.knowns());
assert_eq!(f.cells_with_n_candidates(0), CellSet::from("A3 A7 A8 A9 B2 B5 B7 C8 D1 D4 D5 D9 E4 E6 F1 F5 F6 F9 G1 G2 G3 H1 H2 H3 H5 H8 J1 J2 J3 J7"));
assert_eq!(f.cells_with_n_candidates(1), CellSet::empty());
assert_eq!(
f.cells_with_n_candidates(2),
CellSet::from("A1 A2 A5 D3 D6 D8 E1 F2 F4 J5")
);
assert_eq!(
f.cells_with_n_candidates(3),
CellSet::from("B1 B3 B8 B9 C7 C9 D2 D7 E2 E5 E8 F7 F8 G5 G7 G9 H4 H6 H9 J4 J8 J9")
);
assert_eq!(
f.cells_with_n_candidates(4),
CellSet::from("A4 A6 B6 C1 C2 C3 E3 E7 G4 G6 G8 H7")
);
assert_eq!(
f.cells_with_n_candidates(5),
CellSet::from("B4 C5 C6 E9 F3 J6")
);
assert_eq!(f.cells_with_n_candidates(6), CellSet::from("C4"));
assert_eq!(f.cells_with_n_candidates(7), CellSet::empty());
assert_eq!(f.cells_with_n_candidates(8), CellSet::empty());
assert_eq!(f.cells_with_n_candidates(9), CellSet::empty());
}
#[test]
fn test_cell_candidates_with_n_candidates() {
let f = fixture();
assert_eq!(
f.cell_candidates_with_n_candidates(5).collect_vec(),
vec![
(cell!("B4"), KnownSet::from("2 4 6 7 9")),
(cell!("C5"), KnownSet::from("1 2 6 7 8")),
(cell!("C6"), KnownSet::from("1 2 5 6 8")),
(cell!("E9"), KnownSet::from("2 5 7 8 9")),
(cell!("F3"), KnownSet::from("1 4 5 6 8")),
(cell!("J6"), KnownSet::from("3 5 6 8 9")),
]
);
assert_eq!(
f.cell_candidates_with_n_candidates(6).collect_vec(),
vec![(cell!("C4"), KnownSet::from("1 2 4 5 6 7"))]
);
assert_eq!(
f.cell_candidates_with_n_candidates(7)
.collect_vec()
.is_empty(),
true
);
}
#[test]
fn test_candidate_cells() {
let f = fixture();
assert_eq!(
f.candidate_cells(known!("1")),
CellSet::from("A4 A5 A6 C4 C5 C6 C7 E3 E5 F3 F4 G4 G5 G6 G7 G8 H4 H6 H7")
);
}
#[test]
fn test_house_candidate_cells() {
let f = fixture();
assert_eq!(
f.house_candidate_cells(House::from("R3"), known!("1")),
CellSet::from("C4 C5 C6 C7")
);
}
}