Skip to content

Commit 5180238

Browse files
committed
Optimize by shortcutting candidate selection.
1 parent 96a29a3 commit 5180238

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

go/sudoku/solver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ func (s *solver) candidate() int {
140140
if value == 0 {
141141
conflicts := bits.OnesCount16(s.conflicts[cell])
142142
if conflicts > score {
143+
// candidate() runs when all empty cells have at least 2 choices i.e. at
144+
// most 7 conflicts. Stop searching if we find a cell with 7 conflicts.
145+
if conflicts >= 7 {
146+
return cell
147+
}
143148
candidate = cell
144149
score = conflicts
145150
}

0 commit comments

Comments
 (0)