|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import pandas as pd |
|
import numpy as np |
|
import os |
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PERFORM_CLEANUP = False |
|
|
|
|
|
INPUT_CSV_PATH = 'sudoku.csv' |
|
INVALID_INDICES_FILE = 'invalid_sudoku_indices.txt' |
|
INVALID_CSV_EXPORT_PATH = 'invalid_sudokus.csv' |
|
|
|
|
|
|
|
|
|
|
|
def validate_solution_grid(grid_1d: np.ndarray) -> bool: |
|
""" |
|
Mathematically validates a 1D numpy array representing a 9x9 Sudoku solution. |
|
|
|
Args: |
|
grid_1d: A numpy array of 81 integers. |
|
|
|
Returns: |
|
True if the grid is a valid Sudoku solution, False otherwise. |
|
""" |
|
if grid_1d.shape[0] != 81 or np.any(grid_1d == 0): |
|
|
|
return False |
|
|
|
grid = grid_1d.reshape(9, 9) |
|
|
|
|
|
required_set = set(range(1, 10)) |
|
|
|
|
|
for i in range(9): |
|
if set(grid[i, :]) != required_set: |
|
return False |
|
|
|
|
|
for j in range(9): |
|
if set(grid[:, j]) != required_set: |
|
return False |
|
|
|
|
|
for box_row_start in range(0, 9, 3): |
|
for box_col_start in range(0, 9, 3): |
|
box = grid[box_row_start:box_row_start+3, box_col_start:box_col_start+3] |
|
if set(box.flatten()) != required_set: |
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def run_validation(): |
|
"""Main function to run the validation and reporting process.""" |
|
print("--- Sudoku CSV Validator V2.1 ---") |
|
|
|
if not os.path.exists(INPUT_CSV_PATH): |
|
print(f"FATAL ERROR: The file '{INPUT_CSV_PATH}' was not found.") |
|
print("Please make sure you have generated it using 'Generate_sudokuCSV_V2.1.py'.") |
|
sys.exit(1) |
|
|
|
print(f"Loading data from '{INPUT_CSV_PATH}'...") |
|
try: |
|
df = pd.read_csv(INPUT_CSV_PATH) |
|
|
|
df['solutions_str'] = df['solutions'].astype(str).str.zfill(81) |
|
except Exception as e: |
|
print(f"Error reading CSV file: {e}") |
|
sys.exit(1) |
|
|
|
print("Validating all solution grids...") |
|
valid_indices = [] |
|
invalid_indices = [] |
|
|
|
for index, row in df.iterrows(): |
|
try: |
|
|
|
solution_grid_1d = np.array(list(map(int, row['solutions_str']))) |
|
if validate_solution_grid(solution_grid_1d): |
|
valid_indices.append(index) |
|
else: |
|
invalid_indices.append(index) |
|
except (ValueError, TypeError): |
|
|
|
invalid_indices.append(index) |
|
|
|
|
|
num_valid = len(valid_indices) |
|
num_invalid = len(invalid_indices) |
|
total_grids = len(df) |
|
|
|
print("\n--- VALIDATION REPORT ---") |
|
print(f"Total grids scanned: {total_grids}") |
|
print(f" => Valid solutions: {num_valid}") |
|
print(f" => Invalid solutions: {num_invalid}") |
|
print("-------------------------\n") |
|
|
|
if num_invalid > 0: |
|
print(f"Found {num_invalid} invalid grids. Saving their indices to '{INVALID_INDICES_FILE}'.") |
|
|
|
with open(INVALID_INDICES_FILE, 'w') as f: |
|
for index in invalid_indices: |
|
f.write(f"{index}\n") |
|
|
|
print("\n*** ACTION REQUIRED ***") |
|
print(f"To clean your '{INPUT_CSV_PATH}', please follow these steps:") |
|
print("1. Open this script ('Validate_sudokuCSV_V2.1.py') in the editor.") |
|
print("2. Change the configuration flag at the top from 'PERFORM_CLEANUP = False' to 'PERFORM_CLEANUP = True'.") |
|
print("3. Re-run this script.") |
|
print("This will move the invalid entries to 'invalid_sudokus.csv' and create a clean 'sudoku.csv'.") |
|
else: |
|
print("Congratulations! All solution grids in the CSV are valid.") |
|
|
|
if os.path.exists(INVALID_INDICES_FILE): |
|
os.remove(INVALID_INDICES_FILE) |
|
|
|
|
|
def run_cleanup(): |
|
"""Main function to perform the cleanup process.""" |
|
print("--- Sudoku CSV Cleanup Utility ---") |
|
print(f"PERFORM_CLEANUP is set to True. Attempting to clean '{INPUT_CSV_PATH}'.") |
|
|
|
if not os.path.exists(INVALID_INDICES_FILE): |
|
print(f"ERROR: The file '{INVALID_INDICES_FILE}' was not found.") |
|
print("Please run the script with 'PERFORM_CLEANUP = False' first to generate the list of invalid indices.") |
|
sys.exit(1) |
|
|
|
print(f"Loading master data from '{INPUT_CSV_PATH}'...") |
|
df = pd.read_csv(INPUT_CSV_PATH) |
|
|
|
print(f"Loading invalid indices from '{INVALID_INDICES_FILE}'...") |
|
with open(INVALID_INDICES_FILE, 'r') as f: |
|
invalid_indices = [int(line.strip()) for line in f] |
|
|
|
print(f"Found {len(invalid_indices)} indices to remove.") |
|
|
|
|
|
df_invalid = df.loc[invalid_indices] |
|
df_valid = df.drop(invalid_indices) |
|
|
|
|
|
|
|
print(f"Saving {len(df_invalid)} invalid entries to '{INVALID_CSV_EXPORT_PATH}'...") |
|
df_invalid.to_csv(INVALID_CSV_EXPORT_PATH, index=False) |
|
|
|
|
|
print(f"Overwriting '{INPUT_CSV_PATH}' with {len(df_valid)} valid entries...") |
|
df_valid.to_csv(INPUT_CSV_PATH, index=False) |
|
|
|
|
|
os.remove(INVALID_INDICES_FILE) |
|
|
|
print("\n--- Cleanup Complete! ---") |
|
print(f"Your '{INPUT_CSV_PATH}' is now clean.") |
|
print(f"The invalid entries have been moved to '{INVALID_CSV_EXPORT_PATH}'.") |
|
print("Set 'PERFORM_CLEANUP = False' before running validation again.") |
|
|
|
|
|
if __name__ == '__main__': |
|
if PERFORM_CLEANUP: |
|
run_cleanup() |
|
else: |
|
run_validation() |