File size: 8,043 Bytes
cfec946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Filename: Validate_sudokuCSV_V2.1.py
#
# Description:
# This script validates the 'solutions' column of a sudoku.csv file generated by
# Generate_sudokuCSV_V2.1.py or a similar script. It mathematically checks if each
# solved grid adheres to the rules of Sudoku.
#
# Key Logic:
# 1.  **Loads Data:** Reads the 'sudoku.csv' file into a pandas DataFrame.
# 2.  **Validates Each Solution:** For every row, it checks the 'solutions' grid to ensure:
#     - Each row (1-9) contains exactly one of each digit from 1 to 9.
#     - Each column (1-9) contains exactly one of each digit from 1 to 9.
#     - Each 3x3 box (1-9) contains exactly one of each digit from 1 to 9.
# 3.  **Reports Findings:** Prints a clear summary of how many grids are valid and invalid.
# 4.  **Handles Invalid Grids (Colab-Friendly):**
#     - If invalid grids are found, it saves their row indices to a file named
#       `invalid_sudoku_indices.txt`.
#     - It then instructs the user on how to clean the master CSV by changing a
#       single configuration flag (`PERFORM_CLEANUP`) and re-running the script.
# 5.  **Performs Cleanup (If Enabled):** If the `PERFORM_CLEANUP` flag is set to True
#     and the index file exists, it will:
#     - Move the invalid rows from 'sudoku.csv' to a new 'invalid_sudokus.csv' file.
#     - Overwrite 'sudoku.csv' with a new, clean version.

import pandas as pd
import numpy as np
import os
import sys

# ==============================================================================
# === CONFIGURATION ===
# ==============================================================================

# --- Primary Action ---
# Set this to True AFTER running the script once and seeing an 'invalid' report.
# This will trigger the cleanup process on the next run.
PERFORM_CLEANUP = False

# --- File Paths ---
INPUT_CSV_PATH = 'sudoku.csv'
INVALID_INDICES_FILE = 'invalid_sudoku_indices.txt'
INVALID_CSV_EXPORT_PATH = 'invalid_sudokus.csv'

# ==============================================================================
# === VALIDATION LOGIC ===
# ==============================================================================

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):
        # Must be a fully filled 81-cell grid
        return False

    grid = grid_1d.reshape(9, 9)
    
    # The set of digits {1, 2, 3, 4, 5, 6, 7, 8, 9}
    required_set = set(range(1, 10))

    # 1. Check all rows
    for i in range(9):
        if set(grid[i, :]) != required_set:
            return False

    # 2. Check all columns
    for j in range(9):
        if set(grid[:, j]) != required_set:
            return False

    # 3. Check all 3x3 boxes
    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

    # If all checks pass, the grid is valid
    return True

# ==============================================================================
# === MAIN EXECUTION SCRIPT ===
# ==============================================================================

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)
        # Ensure columns are treated as strings to preserve leading zeros, then convert to numbers
        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:
            # Convert the string of digits into a numpy array of integers
            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):
            # Handle cases where a row might be malformed
            invalid_indices.append(index)

    # --- Report Findings ---
    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}'.")
        # Save indices to the text file, one index per line
        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 no invalid grids were found, remove the old index file if it exists
        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.")

    # Separate the DataFrame into invalid and valid parts
    df_invalid = df.loc[invalid_indices]
    df_valid = df.drop(invalid_indices)

    # --- Perform the file operations ---
    # 1. Export invalid puzzles and their solutions
    print(f"Saving {len(df_invalid)} invalid entries to '{INVALID_CSV_EXPORT_PATH}'...")
    df_invalid.to_csv(INVALID_CSV_EXPORT_PATH, index=False)

    # 2. Overwrite the original file with only the valid data
    print(f"Overwriting '{INPUT_CSV_PATH}' with {len(df_valid)} valid entries...")
    df_valid.to_csv(INPUT_CSV_PATH, index=False)

    # 3. Clean up the index file
    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()