예제 #1
0
        '27  15  8   3  7 4    7     5 1   7   9   2   6   2 5     8    6 5  4   8  59  41',
        '8 64 3    5     7     2    32  8  5   8 5 4  1   7  93    4     9     4    6 72 8',
        '  1 2 7   5     9    4      8   5    9           6   2  2        6     5     9 83',  # sudoku of 9 with only 17 digits http://www2.ic-net.or.jp/~takaken/auto/guest/bbs46.html 
        '1  3    7428      3        5  6   8  84537  6    4 5     482 6   5    7 612 9  3 ',
        '8          36      7  9 2   5   7       457     1   3   1    68  85   1  9    4  ',  # (was) hardest (35s)
        '1       2 9 4   5   6   7   5 9 3       7       85  4 7     6   3   9 8   2     1',  # (was) hard (28s) "easter monster"
        '     6    59     82    8    45        3        6  3 54   325  6                  ',  # hardest  norvig (188 sec, but not a sudoku)
]:

    cnf = []

    # each point assigned exactly one value
    for point in points:
        cnf += one_of(comb(point, value) for value in values)

    # each value gets assigned to exactly one point in each group
    for group in groups:
        for value in values:
            cnf += one_of(comb(point, value) for point in group)

    # add facts for known values in a specific puzzle
    for known in str_to_facts(given):
        cnf += basic_fact(known)

    # solve it and display the results
    result = facts_to_str(solve_one(cnf))
    show(given)
    print()
    show(result)
    print('=-' * 20)
예제 #2
0

cnf = []

# each house gets exactly one value from each attribute group
for house in houses:
    for group in groups:
        cnf += one_of(comb(value, house) for value in group)

# each value gets assigned to exactly one house
for value in values:
    cnf += one_of(comb(value, house) for house in houses)

cnf += same_house('brit', 'red')
cnf += same_house('swede', 'dog')
cnf += same_house('dane', 'tea')
cnf += consecutive('green', 'white')
cnf += same_house('green', 'coffee')
cnf += same_house('pall mall', 'bird')
cnf += same_house('yellow', 'dunhill')
cnf += found_at('milk', 3)
cnf += found_at('norwegian', 1)
cnf += beside('blends', 'cat')
cnf += beside('horse', 'dunhill')
cnf += same_house('blue master', 'root beer')
cnf += same_house('german', 'prince')
cnf += beside('norwegian', 'blue')
cnf += beside('blends', 'water')

pprint(solve_one(cnf))