jimweirich / sudoku

A Simple Sudoku Solver

This URL has Read+Write access

jimweirich (author)
Wed Oct 07 12:31:33 -0700 2009
commit  2147dc66086d61bfd76afdb116db8164955cce41
tree    9c18a24cf52a64caf0d48957eb4f7a39c08e95bb
parent  face3e482fd15bc6ef874c4ed5b5917db12af1bb
sudoku / s.rb
100755 22 lines (20 sloc) 0.349 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env ruby19
 
def r
  (0..80).each { |i|
    next if A[i] > 0
    ((1..9).to_a - (0..80).map { |j|
      (j/9 == i/9 ||
        j%9 == i%9 ||
        j/27 == i/27 && j%9/3 == i%9/3) ? A[j] : 0
    }).each { |g|
      A[i] = g
      r
      A[i] = 0
    }
    return
  }
  puts A.join
  exit
end
A = gets.strip.split(//).map {|n| n.to_i }
r