Skip to content

Commit

Permalink
Extract peer elimination function
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pauley committed Mar 2, 2011
1 parent f47fe62 commit 6b95a06
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions sudoku.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ def eliminate(values, square, digit):
if len(values[square]) == 0: if len(values[square]) == 0:
return False ## Contradiction: removed last value return False ## Contradiction: removed last value


## (1) If a square is reduced to one value d2, then eliminate d2 from the peers. values = peer_eliminate(values, square)
if len(values[square]) == 1: if values is False:
d2 = values[square] return False
if not all(eliminate(values, s2, d2) for s2 in peers[square]):
return False
## (2) If a unit is reduced to only one place for a digit, then put it there ## (2) If a unit is reduced to only one place for a digit, then put it there
for u in units[square]: for u in units[square]:
digit_places = [square2 for square2 in u if digit in values[square2]] digit_places = [square2 for square2 in u if digit in values[square2]]
Expand All @@ -80,6 +78,14 @@ def eliminate(values, square, digit):
return False return False
return values 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): def solve(grid):
return search(parse_grid(grid)) return search(parse_grid(grid))


Expand Down

0 comments on commit 6b95a06

Please sign in to comment.