-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolve.py
executable file
·74 lines (64 loc) · 2.14 KB
/
solve.py
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
#!/bin/python3
import sys
import os
import json
from typing import List, Dict, Any
from Constraints import Constraints
from PuzzleSolver import PuzzleSolver as Solver
from PuzzleState import PuzzleState as State
def print_usage() -> None:
"""
Prints usage information on how to run this program.
"""
# TODO
print("usage")
def process_puzzle(path: str) -> None:
"""
Processes one puzzle.
The necessary steps are: Check the file, validate the contents, run the solver and
print the solutions, if any are found.
"""
if not os.path.isfile(path):
print("{} is not a regular file.".format(path))
return
try:
f = open(path)
json_object = json.load(f)
except OSError as error:
print("An error occurred while opening the file {}".format(path),
file=sys.stderr)
print(error.strerror, file=sys.stderr)
return
except json.JSONDecodeError as error:
print("An error occurred while parsing the JSON file {}".format(path),
file=sys.stderr)
return
else:
f.close()
errors, instance = Constraints.validate_json(json_object)
if errors:
print("The configuration file is not valid.", file=sys.stderr)
print("Errors:", file=sys.stderr)
print("\t", end="", file=sys.stderr)
print("\n\t".join(errors), file=sys.stderr)
return
solver: Solver = Solver(instance)
solutions: List[State] = solver.solve()
first = True
for index, solution in enumerate(solutions):
if not first:
print()
first = False
print("Solution {}/{}".format(index + 1, len(solutions)))
print(solution)
def main() -> None:
if len(sys.argv) < 2 or \
any(help_command in sys.argv for help_command in ["--help", "-h", "-?"]):
print_usage()
return
puzzles = len(sys.argv) - 1
for index, path in enumerate(sys.argv[1:]):
print("Processing puzzle {} of {}".format(index + 1, puzzles))
process_puzzle(path)
if __name__ == "__main__":
main()