Created
December 6, 2018 04:30
-
-
Save regehr/2b4ffd6585065573977b0f7847daf3b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from z3 import * | |
n = 10 | |
vars = [] | |
def getConstraint(v, x, y): | |
if x < 0 or x >= n or y < 0 or y >= n: | |
return False | |
return v == (1 + vars[x][y]) | |
s = SimpleSolver() | |
for x in range(0, n): | |
xl = [] | |
for y in range(0, n): | |
v = Int('n' + str(x) + "x" + str(y)) | |
s.add(v >= 1, v <= (n * n)) | |
xl.append(v) | |
vars.append(xl) | |
for x in range(0, n): | |
for y in range(0, n): | |
v = vars[x][y] | |
c = v == 1 | |
c = Or(c, getConstraint(v, x - 3, y)) | |
c = Or(c, getConstraint(v, x + 3, y)) | |
c = Or(c, getConstraint(v, x, y - 3)) | |
c = Or(c, getConstraint(v, x, y + 3)) | |
c = Or(c, getConstraint(v, x + 2, y + 2)) | |
c = Or(c, getConstraint(v, x + 2, y - 2)) | |
c = Or(c, getConstraint(v, x - 2, y + 2)) | |
c = Or(c, getConstraint(v, x - 2, y - 2)) | |
s.add(c) | |
s.add(Distinct([vars[x][y] for x in range(n) for y in range(n)])) | |
s.add(vars[0][0] == 1) | |
res = s.check() | |
print res | |
if res == sat: | |
m = s.model() | |
for x in range(0, n): | |
for y in range(0, n): | |
v = vars[x][y] | |
print repr(m[v]).rjust(3), | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or just recursion:
It's pretty speedy: