Skip to content

Commit f3a8ec5

Browse files
author
Frederic Vogels
committed
python puzzle verification
1 parent d60e736 commit f3a8ec5

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

python/pc.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import re
99
from picross.grid import Grid
10-
from picross.solver import show, solve_puzzle, derive_constraints, read_image_from_file
10+
from picross.solver import show, solve_puzzle, derive_constraints, read_image_from_file, is_valid_puzzle
1111

1212

1313
def _parse_constraints(string):
@@ -93,7 +93,15 @@ def _extract_puzzle_index(filename):
9393
return int(match.group(1))
9494

9595

96-
def _add_puzzle(archive, author, solution):
96+
def _add_puzzle(archive, author, solution, verify=True):
97+
if verify:
98+
print('Verifying puzzle... (can take a couple of seconds)')
99+
if not is_valid_puzzle(solution):
100+
print(f'{show(solution)}\n\nis not a valid puzzle')
101+
sys.exit(-1)
102+
else:
103+
print('Puzzle deemed valid!')
104+
97105
width = solution.width
98106
height = solution.height
99107

@@ -123,8 +131,9 @@ def _add_puzzle_from_solution(args):
123131
archive = args.archive
124132
author = args.author
125133
solution_file = args.solution_file
134+
verify = args.verify
126135
solution = read_image_from_file(solution_file)
127-
_add_puzzle(archive, author, solution)
136+
_add_puzzle(archive, author, solution, verify=verify)
128137

129138

130139
def _process_command_line_arguments():
@@ -143,6 +152,7 @@ def create_archive_parsers(subparsers):
143152
subparser.add_argument('archive', help='archive', action='store')
144153
subparser.add_argument('author', help='author', action='store')
145154
subparser.add_argument('solution_file', help='file containing solution', action='store')
155+
subparser.add_argument('--no-verify', help='do not check puzzle validity', action='store_false', dest='verify', default=True)
146156
subparser.set_defaults(func=_add_puzzle_from_solution)
147157

148158
subparser = subparsers.add_parser('add-from-constraints', help='adds puzzle to archive; puzzle specified by constraints')

python/picross/solver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def count_unknowns():
8383
return grid
8484

8585

86-
def is_valid_puzzle(column_constraints, row_constraints):
86+
def is_valid_puzzle(puzzle):
87+
column_constraints, row_constraints = derive_constraints(puzzle)
8788
solved = solve_puzzle(column_constraints=column_constraints, row_constraints=row_constraints)
8889
return not solved.contains(UNKNOWN)
8990

0 commit comments

Comments
 (0)