-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution for sudoku strats: hidden singles
- Loading branch information
1 parent
9c760b8
commit a6221fc
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Kata url: https://www.codewars.com/kata/65cd01dda11df609386d6ef9.""" | ||
|
||
def groups(n: int) -> list[tuple[int, int]]: | ||
return [(a,b) for a in (0,n,n*2) for b in (0,n,n*2)] | ||
|
||
|
||
def find_sol(s, val): | ||
for gy, gx in groups(3): | ||
coords = [(gx + x,gy + y) for y, x in groups(1) if s[gy + y][gx + x]] | ||
if len(coords) == 1: | ||
return coords[0][1], coords[0][0], val | ||
|
||
for y in range(9): | ||
if sum(s[y]) == 1: | ||
return y, s[y].index(True), val | ||
if sum(col := [s[x][y] for x in range(9)]) == 1: | ||
return col.index(True), y, val | ||
return False | ||
|
||
|
||
def progress(puzzle: list[list[int]], val = 1) -> tuple[int,int,int]: | ||
s = [[(c == 0) for c in l] for l in puzzle] | ||
|
||
coords = ((x,y) for y in range(9) for x in range(9) if puzzle[y][x] == val) | ||
for x, y in coords: | ||
for n in range(9): | ||
s[y][n] = s[n][x] = False | ||
s[y - (y%3) + (n%3)][x - (x % 3) + (n//3)] = False | ||
|
||
return find_sol(s, val) or progress(puzzle, val = val + 1) |