Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #full
- #xmax = 12 * 22 + 9
- #ymax = 12 * 5 + 9
- #min
- #xmax = 12 * 1 + 9
- #ymax = 12 * 1 + 9
- xmax = 12 * 22 + 9
- ymax = 12 * 5 + 9
- import csv
- def sx(y,x):
- return " x_"+str(y)+"_"+str(x)
- def freecell(y,x):
- return "(int" + sx(y,x) + " 1 9)\n"
- def defcell(y,x,a):
- return "(int" + sx(y,x) + " " + str(a) +" "+ str(a) +")\n"
- def alld_3x3(y,x):
- return ("(alldifferent" + sx(y,x) + sx(y+1,x) + sx(y+2,x)
- + sx(y,x+1) + sx(y+1,x+1) + sx(y+2,x+1)
- + sx(y,x+2) + sx(y+1,x+2) + sx(y+2,x+2) + ")\n")
- def alld_row(y,x):
- return ("(alldifferent" + sx(y,x) + sx(y,x+1) + sx(y,x+2)
- + sx(y,x+3) + sx(y,x+4) + sx(y,x+5)
- + sx(y,x+6) + sx(y,x+7) + sx(y,x+8) + ")\n")
- def alld_col(y,x):
- return ("(alldifferent" + sx(y,x) + sx(y+1,x) + sx(y+2,x)
- + sx(y+3,x) + sx(y+4,x) + sx(y+5,x)
- + sx(y+6,x) + sx(y+7,x) + sx(y+8,x) + ")\n")
- f = open('Q.csv','r')
- g = open('Q.csp','w')
- rd = csv.reader(f)
- #for row in range(0,6):
- # dum = next(rd)
- y = 0
- for y in range(0,ymax):
- row = next(rd)
- for x in range(0,xmax):
- if ((((x <= 2) or ((3 <= x%12) and (x%12 <= 5)) or (xmax-3 <= x)) and
- ((y <= 2) or ((3 <= y%12) and (y%12 <= 5)) or (ymax-3 <= y))) or
- ((9 <= x%12) and (9 <= y%12))):
- print " ",
- continue
- if not('1' <= row[x+1] and row[x+1] <= '9'):
- row[x+1]='-'
- g.write(freecell(y,x))
- if ('1' <= row[x+1] and row[x+1] <= '9'):
- g.write(defcell(y,x,row[x+1]))
- print row[x+1],
- print
- for y in range(0,ymax):
- for x in range(0,xmax):
- if ((((x <= 2) or ((3 <= x%12) and (x%12 <= 5)) or (xmax-3 <= x)) and
- ((y <= 2) or ((3 <= y%12) and (y%12 <= 5)) or (ymax-3 <= y))) or
- ((9 <= x%12) and (9 <= y%12))):
- continue
- if ((x%3 == 0) and (y%3 == 0)):
- #print "3x3",y,x
- g.write(alld_3x3(y,x))
- if (((x%12 == 0) and ((y%12 < 3) or (y%12 > 5)) and (3 < y) and (y < ymax-3)) or
- ((x%12 == 6) and (y%12 < 9) and (x < xmax-9))):
- #print "alld_row",y,x
- g.write(alld_row(y,x))
- if (((y%12 == 0) and ((x%12 < 3) or (5 < x%12)) and (3 < x) and (x < xmax-3)) or
- ((y%12 == 6) and (x%12 < 9) and (y < ymax-9))):
- #print "alld_col",y,x
- g.write(alld_col(y,x))
- f.close()
- g.close()
Add Comment
Please, Sign In to add comment