Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. | 1 2 3 | 4 5 6 | 7 8 9
  2. -+-----------------------
  3. A| 3 | 1 |
  4. B| 6 | | 5
  5. C| 5 | | 9 8 3
  6. -+-----------------------
  7. D| 8 | 6 | 3 2
  8. E| | 5 |
  9. F| 9 3 | 8 | 6
  10. -+-----------------------
  11. G| 7 1 4 | | 9
  12. H| 2 | | 8
  13. I| | 4 | 3
  14.  
  15. | 1 2 3 | 4 5 6 | 7 8 9
  16. -+-----------------------
  17. A| 8 3 2 | 5 9 1 | 6 7 4
  18. B| 4 9 6 | 3 8 7 | 2 5 1
  19. C| 5 7 1 | 2 6 4 | 9 8 3
  20. -+-----------------------
  21. D| 1 8 5 | 7 4 6 | 3 9 2
  22. E| 2 6 7 | 9 5 3 | 4 1 8
  23. F| 9 4 3 | 8 1 2 | 7 6 5
  24. -+-----------------------
  25. G| 7 1 4 | 6 3 8 | 5 2 9
  26. H| 3 2 9 | 1 7 5 | 8 4 6
  27. I| 6 5 8 | 4 2 9 | 1 3 7
  28.  
  29. >sudoku.py "030001000006000050500000983080006302000050000903800060714000009020000800000400030"
  30. 832591674496387251571264983185746392267953418943812765714638529329175846658429137
  31.  
  32. p,:3/:_(p:9:!81)%3
  33. s:{*(,x)(,/{@[x;y;:;]'&21=x[&|/p[;y]=p]?!10}')/&~x}
  34.  
  35. def S(s):
  36. i=s.find('0')
  37. if i<0:print s;return
  38. for v in'123456789':
  39. if sum(v==s[j]and(i/9==j/9or i%9==j%9or(i%9/3==j%9/3and i/27==j/27))for j in range(81))==0:S(s[:i]+v+s[i+1:])
  40. S(raw_input())
  41.  
  42. def f(s):
  43. x=s.find('0')
  44. if x<0:print s;exit()
  45. [c in[(x-y)%9*(x/9^y/9)*(x/27^y/27|x%9/3^y%9/3)or s[y]for y in range(81)]or f(s[:x]+c+s[x+1:])for c in'%d'%5**18]
  46. import sys
  47. f(sys.argv[1])
  48.  
  49. def f(s):
  50. x=s.find('0')
  51. if x<0:print s;exit()
  52. [c in[(x-y)%9*(x/9^y/9)*(x/27^y/27|x%9/3^y%9/3)or s[y]for y in range(81)]or f(s[:x]+c+s[x+1:])for c in'%d'%5**18]
  53. f(raw_input())
  54.  
  55. 'p n'=:(;#)I.0=a=:("."0)Y
  56. ((a p}~3 :'>:?n#9')^:([:(27~:[:+/[:(9=#@~.)"1[:,/(2 2$3),;.3],|:,])9 9$])^:_)a
  57.  
  58. sudoku.pl <<< "030001000006000050500000983080006302000050000903800060714000009020000800000400030"
  59.  
  60. ${/[@_[map{$i-($i="@-")%9+$_,9*$_+$i%9,9*$_%26+$i-$i%3+$i%9-$i%27}0..8%split""]]/o||do$0}for$_=$`.$_.$'.<>,/0/||print..9
  61.  
  62. $_=$s=<>;$r=join$/,map{$n=$_;'.*(?!'.(join'|',map+($_%9==$n%9||int($_/9)==int($n/9)||int($_/27)==int($n/27)&&int($_/3%3)==int($n/3%3)and$_<$n?'\'.($_+1):$_>$n&&substr$s,$_,1)||X,@a).')(.).*'}@a=0..80;s!.!($&||123456789).$/!eg;say/^$r/
  63.  
  64. $ perl -M5.010 golf/sudoku.pl
  65. 030001000006000050500000983080006302000050000903800060714000009020000800000400030
  66. 832591674496387251571264983185746392267953418943812765714638529329175846658429137
  67.  
  68. solve = (sudoku, cell = 0) ->
  69. if cell is 9*9 then return sudoku
  70.  
  71. x = cell%9
  72. y = (cell - x)/9
  73.  
  74. if sudoku[x][y] isnt 0 then return solve sudoku, cell+1
  75.  
  76. row = (i) -> sudoku[x][i]
  77. col = (i) -> sudoku[i][y]
  78. box = (i) -> sudoku[x - x%3 + (i - i%3)/3][y - y%3 + i%3]
  79.  
  80. good = (guess) -> [0...9].every (i) -> guess not in [row(i), col(i), box(i)]
  81.  
  82. guesses = [1..9].filter good
  83.  
  84. solves = (guess) -> sudoku[x][y] = guess; solve sudoku, cell+1
  85.  
  86. (guesses.some solves) or sudoku[x][y] = 0
  87.  
  88. sudoku = [
  89. [1,0,0,0,0,7,0,9,0],
  90. [0,3,0,0,2,0,0,0,8],
  91. [0,0,9,6,0,0,5,0,0],
  92. [0,0,5,3,0,0,9,0,0],
  93. [0,1,0,0,8,0,0,0,2],
  94. [6,0,0,0,0,4,0,0,0],
  95. [3,0,0,0,0,0,0,1,0],
  96. [0,4,0,0,0,0,0,0,7],
  97. [0,0,7,0,0,0,3,0,0]
  98. ]
  99. console.log if solve sudoku then sudoku else 'could not solve'
  100.  
  101. (defn f[o &[x y]](if x(if(> y 8)(apply str(map #(apply str %)o))(first(for[q[(o y)]v(if(=(q x)0)(range 1 10)[(q x)])d[(assoc o y(assoc(o y)x v))]s[(and(every? true?(concat(for[i(range 9)](and(or(not=((d y)i)v)(= i x))(or(not=((d i)x)v)(= i y))))(for[m[#(+ %2(- %(mod % 3)))]r[(range 3)]a r b r c[(m y b)]e[(m x a)]](or(and(= e x)(= c y))(not=((d y)x)((d c)e))))))(f d(mod(+ x 1)9)(if(= x 8)(+ 1 y)y)))]:when s]s)))(f(vec(for[a(partition 9 o)](vec(map #(Integer.(str %))a))))0 0)))
  102.  
  103. (f "030001000006000050500000983080006302000050000903800060714000009020000800000400030")
  104. => "832591674496387251571264983185746392267953418943812765714638529329175846658429137"
  105. (f "004720900039008005001506004040010520028050170016030090400901300100300840007085600")
  106. => "654723981239148765871596234743819526928654173516237498482961357165372849397485612"
  107.  
  108. import std.algorithm,std.range,std.stdio;void main(char[][]args){T s(T)(T p){foreach(i,ref c;p)if(c<49){foreach(o;"123456789".setDifference(chain(p[i/9*9..i/9*9+9],p[i%9..$].stride(9),p[i/27*27+i%9/3*3..$][0..21].chunks(3).stride(3).joiner).array.sort)){c=o&63;if(s(p))return p;}c=48;return[];}return p;}s(args[1]).write;}
  109.  
  110. import std.algorithm, std.range, std.stdio;
  111.  
  112. void main(char[][] args) {
  113. T s(T)(T p) {
  114. foreach (i, ref c; p) if (c < 49) {
  115. foreach (o; "123456789".setDifference(chain(
  116. p[i/9*9..i/9*9+9],
  117. p[i%9..$].stride(9),
  118. p[i/27*27+i%9/3*3..$][0..21].chunks(3).stride(3).joiner
  119. ).array.sort))
  120. {
  121. c = o&63;
  122. if (s(p)) return p;
  123. }
  124. c=48;
  125. return [];
  126. }
  127. return p;
  128. }
  129. s(args[1]).write;
  130. }
  131.  
  132. use integer;@A=split//,<>;sub R{for$i(0..80){next if$A[$i];my%t=map{$_/9==$/9||$_%9==$i%9||$_/27==$i/27&&$_%9/3==$i%9/3?$A[$_]:0=>1}0..80;R($A[$i]=$_)for grep{!$t{$_}}1..9;return$A[$i]=0}die@A}R
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement