def _main(): try: _setup_logging() command_line_arguments = _parse_command_line_arguments() cell_values = PuzzleParser.read_from_file( command_line_arguments.input_file) search_summary = SearchEngine.find_solution( cell_values, command_line_arguments.algorithm, command_line_arguments.timeout_sec) _print_search_summary(search_summary) if command_line_arguments.output_file is not None: GridFormatter.write_to_file(search_summary.final_grid, command_line_arguments.output_file) except InvalidInputError as e: print("Failed to parse the input file {0}: {1}".format( command_line_arguments.input_file, e)) except (InvalidPuzzleError, NoSuchAlgorithmError) as e: print("Puzzle rejected by the search engine: {0}".format(e)) except (FileNotFoundError, IsADirectoryError, PermissionError) as e: print("I/O error: {0}".format(e)) except (ValueError, RuntimeError) as e: print("Unexpected error has occured: {0}".format(e)) finally: print()
def find_solution(puzzle, algorithm, timeout_sec = 10): cell_values = PuzzleParser.read_from_string(puzzle) search_summary = SearchEngine.find_solution(cell_values, algorithm, timeout_sec) formatted_final_grid = GridFormatter.write_to_string(search_summary.final_grid) return (search_summary, formatted_final_grid)