Esempio n. 1
0
def require_different_values(x0, y0, x1, y1, n):
    formulas = []
    # TODO: return a list of logic formulas that together implement the rule
    # "The values of cells (x0,y0) and (x1,y1) must be different"

    for d in range(1, n + 1):
        formulas.append(
            Or([
                And(value(x0, y0, d), Not(value(x1, y1, d))),
                And(Not(value(x0, y0, d)), value(x1, y1, d))
            ]))

    formulas = [Or(formulas)]
    return formulas
Esempio n. 2
0
def alternative_solution_finder(n=9):
    formulas = []
    # TODO: Return a list of formulas that, given
    #   a sudoku puzzle defined by clue(x,y,v) atoms and
    #   a solution to the puzzle defined by origvalue(x,y,v) atoms,
    # finds a different solution to the sudoku expressed using value(x,y,v) atoms.
    # You can assume that clue(x,y,v) -> origvalue(x,y,v) holds true.
    list = []
    k = int(math.sqrt(n))
    for x in range(1, n + 1):
        for y in range(1, n + 1):
            formulas += value_exists(x, y, n)
            formulas += unique_value(x, y, n)
    for x0 in range(1, n + 1):
        for y0 in range(1, n + 1):
            for x1 in range(x0, n + 1):
                for y1 in range(1, n + 1):
                    if x0 == x1 and y0 >= y1:
                        continue
                    if (same_row(x0, y0, x1, y1) or same_col(x0, y0, x1, y1)
                            or same_box(x0, y0, x1, y1, k)):
                        formulas += require_different_values(x0, y0, x1, y1, n)
    for x in range(1, n + 1):
        for y in range(1, n + 1):
            for v in range(1, n + 1):
                list.append(
                    And([
                        value(x, y, v),
                        Implies(clue(x, y, v), value(x, y, v)),
                        Implies(value(x, y, v), Not(origvalue(x, y, v)))
                    ]))
    formulas.append(Or(list))

    return formulas
Esempio n. 3
0
def unique_value(x, y, n):
    formulas = []
    # TODO: return a list of logic formulas that together implement the rule
    # "There can be at most one value at position (x,y)".
    for v1 in range(1, n):
        for v2 in range(v1 + 1, n + 1):
            formulas.append(Or([Not(value(x, y, v1)), Not(value(x, y, v2))]))
    return formulas
Esempio n. 4
0
def unique_value(x, y, n):
    formulas = []
    # TODO: return a list of logic formulas that together implement the rule
    # "There can be at most one value at position (x,y)".

    list = []
    for i in range(1, n + 1):
        for j in range(i + 1, n + 1):
            list.append(Or(Not(value(x, y, i)), Not(value(x, y, j))))
    formulas += [And(list)]
    return formulas
Esempio n. 5
0
def value_exists(x, y, n):
    # Returns a list of logic formulas that together implement the rule
    # "There must exist a value (between 1 and n) at position (x,y)"
    # list=[Or([value(x,y,v) for v in range(1,n+1)])]
    # print(list)
    return [Or([value(x, y, v) for v in range(1, n + 1)])]