import itertools from copy import deepcopy from itertools import combinations, combinations_with_replacement OP_PLUS = "PLUS" OP_MINUS = "MINUS" OP_MULTIPLY = "MULTIPLY" OP_DIVIDE = "DIVIDE" OP_NONE = "NONE" def lambda_mul(x): rv = 1 for v in x: rv *= v return rv OP_LAMBDAS = { OP_PLUS: sum, OP_MINUS: lambda x: abs(x[0] - x[1]), OP_MULTIPLY: lambda_mul, OP_DIVIDE: lambda x: x[0] / x[1] if x[0] % x[1] == 0 else x[1] / x[0], OP_NONE: lambda x: x[0], } BOX_H_LABELS = 'ABCDEF' BOX_V_LABELS = '654321' class Block(object): def __init__(self, boxes, operation, result): self.boxes = boxes self.operation = operation self.result = result self.solutions = set() self.verify() def verify(self): assert self.operation in OP_LAMBDAS if self.operation in [OP_NONE]: assert len(self.boxes) == 1 elif self.operation in [OP_MINUS, OP_DIVIDE]: assert len(self.boxes) == 2 else: assert len(self.boxes) > 1 for box in self.boxes: assert len(box) == 2 assert box[0] in BOX_H_LABELS assert box[1] in BOX_V_LABELS def all_boxes_in_one_row_or_column(self): is_same_column, is_same_row = True, True for box in self.boxes[1:]: if self.boxes[0][0] != box[0]: is_same_column = False if self.boxes[0][1] != box[1]: is_same_row = False return is_same_row or is_same_column def generate_combinations(self, k, n): assert 1 <= k < n # Exploit the structure of the problem # For block of size 2, we don't need comb with replacement as they will # neccesarily be in different columns/rows if k == 2: return list(combinations(range(1, n + 1), k)) # for 3 and more we don't need if all of the boxes are on the # same row or same column elif k > 2: if self.all_boxes_in_one_row_or_column(): return list(combinations(range(1, n + 1), k)) return list(combinations_with_replacement(range(1, n + 1), k)) def generate_hypotheses(self, grid_size): rv = [] op = OP_LAMBDAS[self.operation] hypotheses = self.generate_combinations(k=len(self.boxes), n=grid_size) for hypothesis in hypotheses: if op(hypothesis) == self.result: rv.append(hypothesis) return rv def generate_solutions(self, grid_size): self.generate_hypotheses(grid_size) for hyp in self.generate_hypotheses(grid_size): for perm in itertools.permutations(hyp): sol = tuple(zip(self.boxes, perm)) self.solutions.add(sol) def __repr__(self): return 'Block {}'.format(self.boxes) def translate_box_to_rc(box): return BOX_V_LABELS.index(box[1]), BOX_H_LABELS.index(box[0]) def has_line_integrity(line): existing_values = set() for value in line: if value in existing_values: return False if value != 0: existing_values.add(value) return True def fill_grid(grid, solution): box_values = [] for box_value in solution: box_values.append(box_value) for box, value in box_values: br, bc = translate_box_to_rc(box) grid[br][bc] = value return grid class Game(object): def __init__(self, grid_size, blocks): self.blocks = blocks self.grid_size = grid_size self.verify() for block in self.blocks: block.generate_solutions(self.grid_size) def init_grid(self): return [[0] * self.grid_size for _ in range(self.grid_size)] def verify(self): found = set() for block in self.blocks: for box in block.boxes: assert box not in found found.add(box) assert len(found) == self.grid_size ** 2 def find_blocks_in_row(self, row_num): rv = set() for block in self.blocks: for box in block.boxes: if int(box[1]) == row_num: rv.add(block) return rv def check_grid(self, grid): for row in grid: if not has_line_integrity(row): return False for col_id in range(self.grid_size): col = [grid[r][col_id] for r in range(self.grid_size)] if not has_line_integrity(col): return False return True def solve(self, grid, current_block_id=0): block = self.blocks[current_block_id] for solution in block.solutions: prev_grid = deepcopy(grid) grid = fill_grid(grid, solution) if not self.check_grid(grid): grid = prev_grid else: if current_block_id == len(self.blocks) - 1: return grid sol = self.solve(deepcopy(grid), current_block_id + 1) if sol: return sol def main(): game = Game(grid_size=6, blocks=[ Block(boxes=['A6', 'B6', 'C6', 'D6'], operation=OP_MULTIPLY, result=120), Block(boxes=['E6', 'E5', 'E4', 'F4'], operation=OP_PLUS, result=17), Block(boxes=['F6', 'F5'], operation=OP_PLUS, result=5), Block(boxes=['A5', 'B5'], operation=OP_MINUS, result=1), Block(boxes=['C5', 'C4'], operation=OP_DIVIDE, result=2), Block(boxes=['D5', 'D4'], operation=OP_PLUS, result=3), Block(boxes=['A4', 'A3'], operation=OP_PLUS, result=5), Block(boxes=['B4', 'B3', 'C3'], operation=OP_MULTIPLY, result=30), Block(boxes=['D3', 'D2', 'E2'], operation=OP_MULTIPLY, result=15), Block(boxes=['B2', 'C2', 'C1'], operation=OP_MULTIPLY, result=10), Block(boxes=['A2'], operation=OP_NONE, result=6), Block(boxes=['A1', 'B1'], operation=OP_DIVIDE, result=3), Block(boxes=['D1', 'E1'], operation=OP_MINUS, result=1), Block(boxes=['F1', 'F2', 'F3', 'E3'], operation=OP_PLUS, result=16), ]) grid = game.solve(grid=game.init_grid()) for line in grid: print(line) if __name__ == '__main__': main()