def from_file(cls, image_path: str, with_solution: bool = False): solution_table = CellTable.from_image(image_path) row_instructions = [] column_instructions = [] for i in range(solution_table.rows): row_instructions.append(RowInstructions(solution_table.get_row(i))) for i in range(solution_table.columns): column_instructions.append( RowInstructions(solution_table.get_column(i))) if with_solution: return Board(row_instructions, column_instructions, solution_table) else: return Board(row_instructions, column_instructions)
def instructions_from_file(file_path: str): row_instructions_as_int, column_instructions_as_int = [], [] with open(file_path) as fh: do_row = True for i, line in enumerate(fh): if do_row: if 'COL' in line: do_row = False continue row_instructions_as_int.append(line_to_arr(line)) else: column_instructions_as_int.append(line_to_arr(line)) row_instructions = [] column_instructions = [] for row in row_instructions_as_int: row_instructions.append(RowInstructions.from_list(row)) for column in column_instructions_as_int: column_instructions.append(RowInstructions.from_list(column)) return row_instructions, column_instructions
def data(): return { EMPTY_ROW: CellRow.from_bool([None for i in range(5)]), FALSE_MIDDLE_ROW: CellRow.from_bool([None, None, False, None, None]), FALSE_MID_RIGHT_ROW: CellRow.from_bool([None, None, None, False, None]), FALSE_MID_LEFT_ROW: CellRow.from_bool([None, False, False, None, None]), TRUE_RIGHT_ROW: CellRow.from_bool([None, None, None, None, True]), TURE_RIGHT_FALSE_RIGHT_ROW: CellRow.from_bool([None, None, None, False, True]), FULL_ROW: CellRow.from_bool([True, True, True, True, True]), TRUE_FALSE_ALT_ROW: CellRow.from_bool([True, False, True, False, True]), TURE_MID_FALSE_MID_LEFT_ROW: CellRow.from_bool([None, False, True, None, None]), MUTUAL_TRUE_ROW: CellRow.from_bool([ None, None, None, None, None, None, True, False, None, None, None ]), MUTUAL_TRUE_REVERSE_ROW: CellRow.from_bool([ None, None, None, False, True, None, None, None, None, None, None ]), SINGLE_ONE_INS: RowInstructions.from_list([1]), TWO_ONE_INS: RowInstructions.from_list([1, 1]), THREE_ONE_INS: RowInstructions.from_list([1, 1, 1]), FOUR_ONE_INS: RowInstructions.from_list([1, 1, 1, 1]), ONE_TWO_INS: RowInstructions.from_list([1, 2]), TWO_THREE_INS: RowInstructions.from_list([2, 3]), THREE_TWO_INS: RowInstructions.from_list([3, 2]), FULL_INS: RowInstructions.from_list([5]), THREE_INS: RowInstructions.from_list([3]), }
def can_solution_be_found_(self, location_index: int, instructions: RowInstructions): if instructions is None or len(instructions) == 0: return not self.is_there_fill_in_remainder(location_index) for start_index in range(location_index, len(self.row) - instructions[0] + 1): if start_index not in self.range_manager: continue left_most_range = self.find_left_most_range_for_instruction(start_index, instructions[0]) if left_most_range is None: return False self.left_most_ranges.append(left_most_range) if self.can_solution_be_found_(left_most_range.stop + 1, instructions.get_next()): return True del self.left_most_ranges[-1] if self.row[left_most_range[0]].get_state() == CellState.FILL: return False return False
def is_row_solved_(row: CellRow, instructions: RowInstructions): return RowInstructions.is_row_solved(row, instructions)
def __init__(self, row: CellRow, instructions: RowInstructions): self.left_most_ranges = RowAnalyzer(row, instructions).get_left_most_ranges() self.right_most_ranges = RowAnalyzer(row.reverse(), instructions.reverse()).get_right_most_ranges() self.row = row self.instructions = instructions
def attempt_solve(self, instructions: RowInstructions): if len(self) == 1 and len(self[0]) == instructions.get_min_length(): return self.simple_solution(instructions) return []
def is_column_solved_(self, column: int): return RowInstructions.is_row_solved( self.board.get_column(column), self.board.get_column_instructions(column))
def is_row_solved_(self, row: int): return RowInstructions.is_row_solved( self.board.get_row(row), self.board.get_row_instructions(row))