Repository navigation
Expand file tree
/
Copy pathAsfourSourceCode.py
More file actions
59 lines (43 loc) · 1.46 KB
/
Copy pathAsfourSourceCode.py
File metadata and controls
59 lines (43 loc) · 1.46 KB
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
"""Hyper Sudoku AI Solver — original CLI entry point.
This file preserves the original command-line interface. The solving logic now
lives in the `solver/` package, which provides a cleaner, faster, and tested
implementation along with the Flask-based visual demo (`app.py`).
Usage:
python AsfourSourceCode.py
"""
from __future__ import annotations
import os
import sys
from solver import HyperSudokuBoard, HyperSudokuSolver
def main() -> int:
fil = input("Which file do you want to analyze? ").strip()
in_path = f"input{fil}.txt"
out_path = f"output{fil}.txt"
if not os.path.exists(in_path):
print(f"Error: {in_path} not found.", file=sys.stderr)
return 1
with open(in_path) as f:
text = f.read()
board = HyperSudokuBoard.from_text(text)
print("Initial board:")
print(board)
print()
result = HyperSudokuSolver(board, record_steps=False).solve()
if not result.stats.solved:
print("No solution found.")
return 2
print("Solved board:")
print(result.board)
print()
print(
f"Stats — assignments: {result.stats.assignments}, "
f"backtracks: {result.stats.backtracks}, "
f"time: {result.stats.elapsed_ms:.1f} ms"
)
with open(out_path, "w") as f:
for row in result.board.grid:
f.write(" ".join(str(v) for v in row) + "\n")
print(f"Wrote solution to {out_path}")
return 0
if __name__ == "__main__":
sys.exit(main())