Compare commits

...

7 Commits

Author SHA1 Message Date
Saket Saurav
fe908d59ae
Merge 4279985962 into f3f32ae3ca 2024-11-20 11:22:31 +01:00
pre-commit-ci[bot]
f3f32ae3ca
[pre-commit.ci] pre-commit autoupdate (#12385)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.3 → v0.7.4](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.3...v0.7.4)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-18 22:07:12 +01:00
Christian Clauss
e3bd7721c8
validate_filenames.py Shebang python for Windows (#12371) 2024-11-15 14:59:14 +01:00
pre-commit-ci[bot]
e3f3d668be
[pre-commit.ci] pre-commit autoupdate (#12370)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.2 → v0.7.3](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.2...v0.7.3)
- [github.com/abravalheri/validate-pyproject: v0.22 → v0.23](https://github.com/abravalheri/validate-pyproject/compare/v0.22...v0.23)

* Update sudoku_solver.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2024-11-11 21:05:50 +01:00
pre-commit-ci[bot]
3e9ca92ca9
[pre-commit.ci] pre-commit autoupdate (#12349)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.7.1 → v0.7.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.7.1...v0.7.2)
- [github.com/tox-dev/pyproject-fmt: v2.4.3 → v2.5.0](https://github.com/tox-dev/pyproject-fmt/compare/v2.4.3...v2.5.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-11-04 21:09:03 +01:00
pre-commit-ci[bot]
4279985962 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2024-10-11 14:42:36 +00:00
Saket Saurav
9daa2bbb3d
Create sodukuSolver 2024-10-11 20:11:32 +05:30
4 changed files with 81 additions and 5 deletions

View File

@ -16,7 +16,7 @@ repos:
- id: auto-walrus
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.1
rev: v0.7.4
hooks:
- id: ruff
- id: ruff-format
@ -29,7 +29,7 @@ repos:
- tomli
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "v2.4.3"
rev: "v2.5.0"
hooks:
- id: pyproject-fmt
@ -42,7 +42,7 @@ repos:
pass_filenames: false
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.22
rev: v0.23
hooks:
- id: validate-pyproject

76
backtracking/sodukuSolver Normal file
View File

@ -0,0 +1,76 @@
class Solution(object):
def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: None Do not return anything, modify board in-place instead.
"""
# Helper functions for bit manipulation
def char_to_index(ch):
return ord(ch) - ord('1')
def index_to_char(index):
return chr(index + ord('1'))
# Initialize bitmasks for rows, columns, and boxes
rows = [0] * 9
cols = [0] * 9
boxes = [0] * 9
# Populate bitmasks with initial board values
for r in range(9):
for c in range(9):
if board[r][c] != '.':
num = char_to_index(board[r][c])
bit = 1 << num
rows[r] |= bit
cols[c] |= bit
boxes[(r // 3) * 3 + (c // 3)] |= bit
# Check if placing a number is valid
def is_valid(row, col, num):
bit = 1 << num
box_index = (row // 3) * 3 + (col // 3)
return not (rows[row] & bit or cols[col] & bit or boxes[box_index] & bit)
# Forward Checking to find the most constrained cell
def select_cell():
min_possibilities = float('inf')
cell = None
for row in range(9):
for col in range(9):
if board[row][col] == '.':
possibilities = 0
for num in range(9):
if is_valid(row, col, num):
possibilities += 1
if possibilities < min_possibilities:
min_possibilities = possibilities
cell = (row, col)
return cell
# Recursive function to solve the board
def solve():
cell = select_cell()
if cell is None:
return True # All cells are filled
row, col = cell
for num in range(9):
if is_valid(row, col, num):
bit = 1 << num
board[row][col] = index_to_char(num)
rows[row] |= bit
cols[col] |= bit
boxes[(row // 3) * 3 + (col // 3)] |= bit
if solve():
return True
board[row][col] = '.'
rows[row] &= ~bit
cols[col] &= ~bit
boxes[(row // 3) * 3 + (col // 3)] &= ~bit
return False
solve()

View File

@ -172,7 +172,7 @@ def solved(values):
def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115
return open(filename).read().strip().split(sep)
def random_puzzle(assignments=17):

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3
#!python
import os
try: