Guest User

Untitled

a guest
Oct 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #full
  4. #xmax = 12 * 22 + 9
  5. #ymax = 12 * 5 + 9
  6. #min
  7. #xmax = 12 * 1 + 9
  8. #ymax = 12 * 1 + 9
  9. xmax = 12 * 22 + 9
  10. ymax = 12 * 5 + 9
  11.  
  12. import csv
  13.  
  14. def sx(y,x):
  15. return " x_"+str(y)+"_"+str(x)
  16.  
  17. def freecell(y,x):
  18. return "(int" + sx(y,x) + " 1 9)\n"
  19.  
  20. def defcell(y,x,a):
  21. return "(int" + sx(y,x) + " " + str(a) +" "+ str(a) +")\n"
  22.  
  23. def alld_3x3(y,x):
  24. return ("(alldifferent" + sx(y,x) + sx(y+1,x) + sx(y+2,x)
  25. + sx(y,x+1) + sx(y+1,x+1) + sx(y+2,x+1)
  26. + sx(y,x+2) + sx(y+1,x+2) + sx(y+2,x+2) + ")\n")
  27.  
  28. def alld_row(y,x):
  29. return ("(alldifferent" + sx(y,x) + sx(y,x+1) + sx(y,x+2)
  30. + sx(y,x+3) + sx(y,x+4) + sx(y,x+5)
  31. + sx(y,x+6) + sx(y,x+7) + sx(y,x+8) + ")\n")
  32.  
  33. def alld_col(y,x):
  34. return ("(alldifferent" + sx(y,x) + sx(y+1,x) + sx(y+2,x)
  35. + sx(y+3,x) + sx(y+4,x) + sx(y+5,x)
  36. + sx(y+6,x) + sx(y+7,x) + sx(y+8,x) + ")\n")
  37.  
  38. f = open('Q.csv','r')
  39. g = open('Q.csp','w')
  40.  
  41. rd = csv.reader(f)
  42.  
  43. #for row in range(0,6):
  44. # dum = next(rd)
  45.  
  46. y = 0
  47. for y in range(0,ymax):
  48. row = next(rd)
  49. for x in range(0,xmax):
  50. if ((((x <= 2) or ((3 <= x%12) and (x%12 <= 5)) or (xmax-3 <= x)) and
  51. ((y <= 2) or ((3 <= y%12) and (y%12 <= 5)) or (ymax-3 <= y))) or
  52. ((9 <= x%12) and (9 <= y%12))):
  53. print " ",
  54. continue
  55.  
  56. if not('1' <= row[x+1] and row[x+1] <= '9'):
  57. row[x+1]='-'
  58. g.write(freecell(y,x))
  59.  
  60. if ('1' <= row[x+1] and row[x+1] <= '9'):
  61. g.write(defcell(y,x,row[x+1]))
  62.  
  63. print row[x+1],
  64.  
  65. print
  66.  
  67. for y in range(0,ymax):
  68. for x in range(0,xmax):
  69. if ((((x <= 2) or ((3 <= x%12) and (x%12 <= 5)) or (xmax-3 <= x)) and
  70. ((y <= 2) or ((3 <= y%12) and (y%12 <= 5)) or (ymax-3 <= y))) or
  71. ((9 <= x%12) and (9 <= y%12))):
  72. continue
  73.  
  74. if ((x%3 == 0) and (y%3 == 0)):
  75. #print "3x3",y,x
  76. g.write(alld_3x3(y,x))
  77.  
  78. if (((x%12 == 0) and ((y%12 < 3) or (y%12 > 5)) and (3 < y) and (y < ymax-3)) or
  79. ((x%12 == 6) and (y%12 < 9) and (x < xmax-9))):
  80. #print "alld_row",y,x
  81. g.write(alld_row(y,x))
  82.  
  83. if (((y%12 == 0) and ((x%12 < 3) or (5 < x%12)) and (3 < x) and (x < xmax-3)) or
  84. ((y%12 == 6) and (x%12 < 9) and (y < ymax-9))):
  85. #print "alld_col",y,x
  86. g.write(alld_col(y,x))
  87.  
  88. f.close()
  89. g.close()
Add Comment
Please, Sign In to add comment