def sort(lst1, lst2): """ First, the variables of the collection π π°ππΈπ°π±π»π΄π 2 correspond to a permutation of the variables of π π°ππΈπ°π±π»π΄π 1. Second, the variables of π π°ππΈπ°π±π»π΄π 2 are sorted in increasing order. """ _, ys = satx.permutations(lst1, len(lst1)) satx.apply_single(lst2, lambda i, t: t == ys[i], indexed=True) satx.apply_dual(lst2, lambda a, b: a <= b)
import satx satx.engine(bits=80, cnf_path='4d_perfect_euler_bricks.cnf', simplify=True, signed=True) a = satx.integer() b = satx.integer() c = satx.integer() d = satx.integer() p = satx.integer() q = satx.integer() r = satx.integer() s = satx.integer() t = satx.integer() u = satx.integer() v = satx.integer() satx.apply_single([a, b, c, d, p, q, r, s, t, u, v], lambda x: x > 0) assert a ** 2 + b ** 2 == p ** 2 assert a ** 2 + c ** 2 == q ** 2 assert b ** 2 + c ** 2 == r ** 2 assert a ** 2 + d ** 2 == s ** 2 assert b ** 2 + d ** 2 == t ** 2 assert c ** 2 + d ** 2 == u ** 2 assert a ** 2 + b ** 2 + c ** 2 + d ** 2 == v ** 2 if satx.satisfy(solver='./slime', log=True): print(a, b, c, d, p, q, r, s, t, u, v) else: print('Infeasible...')
import satx satx.engine(bits=80, cnf_path='3d_perfect_euler_bricks.cnf', simplify=True, signed=True) a = satx.integer() b = satx.integer() c = satx.integer() p = satx.integer() q = satx.integer() r = satx.integer() s = satx.integer() satx.apply_single([a, b, c, p, q, r, s], lambda x: x > 0) assert a ** 2 + b ** 2 == p ** 2 assert a ** 2 + c ** 2 == q ** 2 assert b ** 2 + c ** 2 == r ** 2 assert a ** 2 + b ** 2 + c ** 2 == s ** 2 if satx.satisfy(solver='./slime', log=True): print(a, b, c, p, q, r, s) else: print('Infeasible...')
""" Dad wants one-cent, two-cent, three-cent, five-cent, and ten-cent stamps. He said to get four each of two sorts and three each of the others, but I've forgotten which. He gave me exactly enough to buy them; just these dimes." How many stamps of each type does Dad want? A dime is worth ten cents. -- J.A.H. Hunter """ import satx satx.engine(10, cnf_path='aux.cnf') # x is the number of dimes x = satx.integer() # s[i] is the number of stamps of value 1, 2, 3, 5 and 10 according to i s = satx.vector(size=5) satx.apply_single(s, lambda t: t.is_in([3, 4])) # 26 is a safe upper bound assert x <= 26 assert satx.dot(s, [1, 2, 3, 5, 10]) == x * 10 while satx.satisfy('slime'): print(s, x)
""" See http://en.wikibooks.org/wiki/Puzzles/Arithmetical_puzzles/Digits_of_the_Square There is one four-digit whole number x, such that the last four digits of x^2 are in fact the original number x. What is it? """ import satx satx.engine(30, cnf_path='aux.cnf') # x is the number we look for x = satx.integer() # d[i] is the ith digit of x d = satx.vector(size=4) satx.apply_single(d, lambda t: t.is_in(range(10))) assert 1000 <= x < 10000 assert satx.dot(d, [1000, 100, 10, 1]) == x assert (x * x) % 10000 == x if satx.satisfy('slime'): print(x, d) else: print('Infeasible...')
There are 5 non trivial numbers for base 10, and the highest such number is formed of 5 digits. Below, the model is given for base 10. """ from math import ceil import satx # for base 10 n_digits = 5 satx.engine((10**n_digits).bit_length(), cnf_path='aux.cnf') # n is a (non-trivial) Dudeney number n = satx.integer() # s is the perfect cubic root of n s = satx.integer() # d[i] is the ith digit of the Dudeney number d = satx.vector(size=n_digits) satx.apply_single(d, lambda t: t < 10) assert 2 <= n < 10**n_digits assert s < ceil((10**n_digits)**(1 / 3)) + 1 assert n == s * s * s assert sum(d) == s assert satx.dot(d, [10**(n_digits - i - 1) for i in range(n_digits)]) == n while satx.satisfy('slime'): print(n, s, d)
""" See https://en.wikipedia.org/wiki/Verbal_arithmetic A model for a general form of this problem is in CryptoPuzzle.py """ import satx satx.engine(16, cnf_path='aux.cnf') # letters[i] is the digit of the ith letter involved in the equation s, e, n, d, m, o, r, y = letters = satx.vector(size=8) satx.apply_single(letters, lambda t: t < 10) # letters are given different values satx.all_different(letters) # words cannot start with 0 assert s > 0 assert m > 0 # respecting the mathematical equation assert satx.dot([s, e, n, d], [1000, 100, 10, 1]) + satx.dot([m, o, r, e], [1000, 100, 10, 1]) == satx.dot([m, o, n, e, y], [10000, 1000, 100, 10, 1]) if satx.satisfy('slime'): print(letters) else: print('Infeasible...')
""" import itertools import satx n, m = 5, 8 opt = 1 while True: print('OPTIMAL? : {}'.format(opt)) satx.engine(opt.bit_length(), cnf_path='aux.cnf') # x[i][j] is the color at row i and column j x = satx.matrix(dimensions=(n, m)) # at least one corners of different color for any rectangle inside the board for i1, i2 in itertools.combinations(range(n), 2): for j1, j2 in itertools.combinations(range(m), 2): assert satx.one_of([x[i1][j1], x[i1][j2], x[i2][j1], x[i2][j2]]) != \ satx.one_of([x[i1][j1], x[i1][j2], x[i2][j1], x[i2][j2]]) satx.apply_single(satx.flatten(x), lambda t: t < opt) if satx.satisfy('slime'): print(x) break else: opt += 1
See "Constraint Programming in Oz. A Tutorial" by C. Schulte and G. Smolka, 2001 A kid goes into a grocery store and buys four items. The cashier charges $7.11, the kid pays and is about to leave when the cashier calls the kid back, and says ``Hold on, I multiplied the four items instead of adding them; I'll try again; Hah, with adding them the price still comes to $7.11''. What were the prices of the four items? """ import satx # 711 * 100 * 100 * 100 -> 30 bits satx.engine(30, cnf_path='aux.cnf') # x[i] is the price (multiplied by 100) of the ith item x = satx.vector(size=4) satx.apply_single(x, lambda t: t < 711) # adding the prices of items corresponds to 711 cents assert sum(x) == 711 # multiplying the prices of items corresponds to 711 cents (times 1000000) assert x[0] * x[1] * x[2] * x[3] == 711 * 100 * 100 * 100 if satx.satisfy('slime'): print(x) else: print('Infeasible...')
import satx satx.engine(32, cnf_path='aux.cnf') mapping = {1: 'r', 2: 'ry', 3: 'g', 4: 'y'} R, RY, G, Y = 1, 2, 3, 4 table = [(R, R, G, G), (RY, R, Y, R), (G, G, R, R), (Y, R, RY, R)] # v[i] is the color for the ith vehicle traffic light v = satx.vector(size=4) # p[i] is the color for the ith pedestrian traffic light p = satx.vector(size=4) satx.apply_single(v, lambda t: t.is_in([R, RY, G, Y])) satx.apply_single(p, lambda t: t.is_in([R, G])) for i in range(4): assert satx.dot([v[i], p[i], v[(i + 1) % 4], p[(i + 1) % 4]], [1, 10, 100, 1000]) == satx.one_of( [satx.dot(t, [1, 10, 100, 1000]) for t in table]) while satx.satisfy('kissat'): vv = [mapping[t.value] for t in v] pp = [mapping[t.value] for t in p] for a, b in zip(vv, pp): print(a, b, end=', ') print() print(80 * '-')