def attempt_solve(): if is_invalid_input: return for i in range(9): for j in range(9): if grid[i][j] == 0: grid_text_color[i][j] = black logic.solve(grid)
def solve(grid, i, j): while grid[i][j] != 0: if i < 8: i += 1 elif i == 8 and j < 8: i = 0 j += 1 elif i == 8 and j == 8: return True pygame.event.pump() for it in range(1, 10): if valid(grid, i, j, it) == True: grid[i][j] = it global x, y x = i y = j #white color background\ screen.fill((255, 255, 255)) draw() draw_box() pygame.display.update() pygame.time.delay(20) if solve(grid, i, j) == 1: return True else: grid[i][j] = 0 #white color background\ screen.fill((255, 255, 255)) draw() draw_box() pygame.display.update() pygame.time.delay(10) return False
def place(self, val): row, col = self.selected if self.cubes[row][col].value == 0: self.cubes[row][col].set(val) self.update_model() if valid(self.model, val, (row, col)) and solve(self.model): return True else: self.cubes[row][col].set(0) self.cubes[row][col].set_temp(0) self.update_model() return False
async def solve_request(message: types.Message): print(message.text) try: result, pretty = solve(message.text.lower()) result = f'Query: <code>{pretty}</code>\nResult: <code>{result}</code>' print(result) except TimeoutException as e: result = e.message except OverflowError: result = 'Result is too big to handle' except BracesMatchError as e: result = e.message except TypeError as e: result = str(e) except ValueError as e: result = str(e) await message.reply(result, parse_mode='HTML')
async def inline_math_query(inline: types.InlineQuery): try: pretty = inline.query result, pretty = solve(inline.query) except TimeoutException as e: result = e.message except OverflowError: result = 'Result is too big to handle' except BracesMatchError as e: result = e.message except TypeError as e: result = str(e) except ValueError as e: result = str(e) result_pretty = types.InputTextMessageContent( f'Query: <code>{pretty}</code>\nResult: <code>{result}</code>', parse_mode='HTML') item = types.InlineQueryResultArticle(id='1', title=result, input_message_content=result_pretty) await config.bot.answer_inline_query(inline.id, results=[item], cache_time=60)
[0, 0, 0, 0, 0, 0, 0, 0, 0]] if event.key == pygame.K_d: rs = 0 err = 0 fl = 0 grid = [[7, 8, 0, 4, 0, 0, 1, 2, 0], [6, 0, 0, 0, 7, 5, 0, 0, 9], [0, 0, 0, 6, 0, 1, 0, 7, 8], [0, 0, 7, 0, 4, 0, 2, 6, 0], [0, 0, 1, 0, 5, 0, 9, 3, 0], [9, 0, 4, 0, 6, 0, 0, 0, 5], [0, 7, 0, 3, 0, 0, 0, 1, 2], [1, 2, 0, 0, 0, 7, 4, 0, 0], [0, 4, 9, 2, 0, 6, 0, 0, 7]] if fl == 1: if solve(grid, 0, 0) == False: err = 1 else: rs = 1 fl = 0 if val != 0: draw_val(val) #print(x) #print(y) if valid(grid, int(x), int(y), val) == True: grid[int(x)][int(y)] = val k = 0 else: grid[int(x)][int(y)] = 0 raise_error1() val = 0
def solve(self, query, evidence=None, test=False): self.n += 1 sdd, shape = self.get_sdd(query, test) solution = solve(self, sdd, shape) self.clear() return solution
def autoComplete(): board.resetBoard() solve(board) redrawWindow(0)
def main(): """Run main program.""" model = cp_model.CpModel() previous_shift_vars = create_previous_shift_vars(num_days, model, shifts, staff, shift_days) shift_vars = create_shift_vars(previous_shift_vars, model, staff, shifts, shift_days) enforce_shifts_already_worked(staff, previous_shifts, shifts, shift_days, model, shift_vars, num_days) days_in_partial_sequence = 7 valid_shift_sequence_permutations = get_valid_shift_sequence_permutations( valid_shift_sequences, days_in_partial_sequence, num_days, shift_days, shifts, ) enforce_shift_sequences( staff, shift_vars, shifts, shift_days, num_days, model, valid_shift_sequence_permutations, ) skill_mix_vars = create_skill_mix_vars(model, shifts, shift_days, skill_mix_rules) enforce_one_skill_mix_rule_per_shift(shifts, shift_days, skill_mix_vars, skill_mix_rules, model) enforce_skill_mix_rules( shifts, skill_mix_rules, shift_days, model, shift_vars, staff, skill_mix_vars, ) enforce_completion_of_shift_segments( valid_shift_sequences, days_in_partial_sequence, previous_shifts, shift_vars, model, staff, shift_days, shifts, ) max_unpleasant_shifts = configure_objective( model, shift_vars, staff, unpleasant_shifts, num_days, shift_days, ) log.info("Starting solver....") solver = solve(model) display_shifts_by_staff( num_days, shifts, shift_days, staff, shift_vars, solver, max_unpleasant_shifts, )