Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- | 1 2 3 | 4 5 6 | 7 8 9
- -+-----------------------
- A| 3 | 1 |
- B| 6 | | 5
- C| 5 | | 9 8 3
- -+-----------------------
- D| 8 | 6 | 3 2
- E| | 5 |
- F| 9 3 | 8 | 6
- -+-----------------------
- G| 7 1 4 | | 9
- H| 2 | | 8
- I| | 4 | 3
- | 1 2 3 | 4 5 6 | 7 8 9
- -+-----------------------
- A| 8 3 2 | 5 9 1 | 6 7 4
- B| 4 9 6 | 3 8 7 | 2 5 1
- C| 5 7 1 | 2 6 4 | 9 8 3
- -+-----------------------
- D| 1 8 5 | 7 4 6 | 3 9 2
- E| 2 6 7 | 9 5 3 | 4 1 8
- F| 9 4 3 | 8 1 2 | 7 6 5
- -+-----------------------
- G| 7 1 4 | 6 3 8 | 5 2 9
- H| 3 2 9 | 1 7 5 | 8 4 6
- I| 6 5 8 | 4 2 9 | 1 3 7
- >sudoku.py "030001000006000050500000983080006302000050000903800060714000009020000800000400030"
- 832591674496387251571264983185746392267953418943812765714638529329175846658429137
- p,:3/:_(p:9:!81)%3
- s:{*(,x)(,/{@[x;y;:;]'&21=x[&|/p[;y]=p]?!10}')/&~x}
- def S(s):
- i=s.find('0')
- if i<0:print s;return
- for v in'123456789':
- 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:])
- S(raw_input())
- def f(s):
- x=s.find('0')
- if x<0:print s;exit()
- [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]
- import sys
- f(sys.argv[1])
- def f(s):
- x=s.find('0')
- if x<0:print s;exit()
- [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]
- f(raw_input())
- 'p n'=:(;#)I.0=a=:("."0)Y
- ((a p}~3 :'>:?n#9')^:([:(27~:[:+/[:(9=#@~.)"1[:,/(2 2$3),;.3],|:,])9 9$])^:_)a
- sudoku.pl <<< "030001000006000050500000983080006302000050000903800060714000009020000800000400030"
- ${/[@_[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
- $_=$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/
- $ perl -M5.010 golf/sudoku.pl
- 030001000006000050500000983080006302000050000903800060714000009020000800000400030
- 832591674496387251571264983185746392267953418943812765714638529329175846658429137
- solve = (sudoku, cell = 0) ->
- if cell is 9*9 then return sudoku
- x = cell%9
- y = (cell - x)/9
- if sudoku[x][y] isnt 0 then return solve sudoku, cell+1
- row = (i) -> sudoku[x][i]
- col = (i) -> sudoku[i][y]
- box = (i) -> sudoku[x - x%3 + (i - i%3)/3][y - y%3 + i%3]
- good = (guess) -> [0...9].every (i) -> guess not in [row(i), col(i), box(i)]
- guesses = [1..9].filter good
- solves = (guess) -> sudoku[x][y] = guess; solve sudoku, cell+1
- (guesses.some solves) or sudoku[x][y] = 0
- sudoku = [
- [1,0,0,0,0,7,0,9,0],
- [0,3,0,0,2,0,0,0,8],
- [0,0,9,6,0,0,5,0,0],
- [0,0,5,3,0,0,9,0,0],
- [0,1,0,0,8,0,0,0,2],
- [6,0,0,0,0,4,0,0,0],
- [3,0,0,0,0,0,0,1,0],
- [0,4,0,0,0,0,0,0,7],
- [0,0,7,0,0,0,3,0,0]
- ]
- console.log if solve sudoku then sudoku else 'could not solve'
- (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)))
- (f "030001000006000050500000983080006302000050000903800060714000009020000800000400030")
- => "832591674496387251571264983185746392267953418943812765714638529329175846658429137"
- (f "004720900039008005001506004040010520028050170016030090400901300100300840007085600")
- => "654723981239148765871596234743819526928654173516237498482961357165372849397485612"
- 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;}
- 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;
- }
- 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