Permalink
Browse files

Extract peer elimination function

  • Loading branch information...
1 parent f47fe62 commit 6b95a06b7a67cf9fe689c2740fc6c0a5b0b7e4c3 @apauley committed Mar 2, 2011
Showing with 11 additions and 5 deletions.
  1. +11 −5 sudoku.py
View
16 sudoku.py
@@ -64,11 +64,9 @@ def eliminate(values, square, digit):
if len(values[square]) == 0:
return False ## Contradiction: removed last value
- ## (1) If a square is reduced to one value d2, then eliminate d2 from the peers.
- if len(values[square]) == 1:
- d2 = values[square]
- if not all(eliminate(values, s2, d2) for s2 in peers[square]):
- return False
+ values = peer_eliminate(values, square)
+ if values is False:
+ return False
## (2) If a unit is reduced to only one place for a digit, then put it there
for u in units[square]:
digit_places = [square2 for square2 in u if digit in values[square2]]
@@ -80,6 +78,14 @@ def eliminate(values, square, digit):
return False
return values
+def peer_eliminate(values, square):
+ ## (1) If a square is reduced to one value, then eliminate it from the peers.
+ if len(values[square]) == 1:
+ digit = values[square]
+ if not all(eliminate(values, s2, digit) for s2 in peers[square]):
+ return False
+ return values
+
def solve(grid):
return search(parse_grid(grid))

0 comments on commit 6b95a06

Please sign in to comment.