-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (54 loc) · 2.29 KB
/
main.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
import argparse
from colorama import init
from src.solver import solve
from src.visualizer import answer_to_visual, \
green_color_string, red_color_string
from src.file_parser import parse_file
from src.puzzle_generator import generate_puzzle
def execute_gui_script():
# TODO
raise NotImplementedError
def execute_cli_script():
def config_cmd():
config = argparse.ArgumentParser(
description='Solve Shikaku - returns a '
'solution of shikaku puzzle')
config.add_argument('-f', '--file', type=argparse.FileType(mode='r'),
help="Provide a name of file with puzzle grid. "
"Empty squares should be marked as '-'. "
"Squares must be separated "
"by space in each row.")
config.add_argument('-n', '--number', type=int, dest='n',
help='Provide a number of solutions to show')
return config
init()
arguments = config_cmd().parse_args()
if arguments.file is None:
generate_puzzle("src/generated_puzzle.txt")
with open("src/generated_puzzle.txt", "r") as file:
print(green_color_string("Generated puzzle:"))
print(*list(file), sep='')
file.seek(0)
numbers_list, size_x, size_y = parse_file(file)
print(green_color_string("Solutions:"))
else:
numbers_list, size_x, size_y = parse_file(arguments.file)
if not size_y:
print(red_color_string('Provided file is empty.'))
quit()
answers = []
for numbers in numbers_list:
answers.extend(solve(numbers, size_x, size_y))
if not answers:
print(red_color_string('This puzzle has no solution.'))
quit()
solutions_count = len(answers) if arguments.n is None else arguments.n
if solutions_count > len(answers):
s = 's' if len(answers) > 1 else ''
print(green_color_string(f'There are only {len(answers)} solution{s}'))
for i in range(min(solutions_count, len(answers))):
for line in answer_to_visual(answers[i], size_x, size_y):
print(*line)
print()
if __name__ == "__main__":
execute_cli_script()