Dirk M. wrote:

i wouldn’t call it concise, it simply doesn’t work… probably an error

in porting to Ruby…

It works. This is the complete code.

Christer

# Author: Kevin Greer (JavaScript), Date: Dec 25, 2005 – Copyright

2005, All Rights Reserved

# Rewritten in Ruby by Christer N. 2005-12-29

class Sudoku

def initialize s

@s=s

end

def display

for a in 0…2

for b in 0…2

for c in 0…2

for d in 0…2

print " " + @s[a][b][c][d].to_s

end

print " |" if c<2

end

print “\n”

end

print “-------±------±------\n” if a<2

end

end

def solve a, b, c, d

return true if a==3

return solve(a+1, 0, c, d) if b==3

return solve(a, b+1, 0, d) if c==3

return solve(a, b, c+1, 0) if d==3

return solve(a, b, c, d+1) if @s[a][b][c][d]!=0

```
for digit in 1..9
if not occupied?(a, b, c, d, digit) then
@s[a][b][c][d] = digit
return true if solve(a, b, c, d+1)
@s[a][b][c][d] = 0
end
end
false
```

end

def occupied? a, b, c, d, digit

for x in 0…2

for y in 0…2

return true if @s[a][b][x][y]==digit # block

return true if @s[a][x][c][y]==digit # row

return true if @s[x][b][y][d]==digit # column

end

end

false

end

end

s =

[[[[0,0,0],[0,7,1],[0,0,5]],

[[5,0,0],[0,6,9],[0,7,1]],

[[0,7,1],[8,5,3],[4,2,0]]],

[[[0,1,0],[0,0,2],[0,0,0]],

[[7,8,0],[1,5,4],[0,9,2]],

[[0,4,0],[3,6,0],[1,8,0]]],

[[[0,6,4],[0,2,3],[0,5,0]],

[[9,0,5],[0,1,0],[0,0,0]],

[[7,0,0],[5,9,0],[0,0,0]]]]

sudoku = Sudoku.new(s)

sudoku.display

print “\nsolving…\n\n”

if sudoku.solve(0,0,0,0) then

sudoku.display

end

0 0 0 | 0 7 1 | 0 0 5

5 0 0 | 0 6 9 | 0 7 1

0 7 1 | 8 5 3 | 4 2 0

-------±------±------

0 1 0 | 0 0 2 | 0 0 0

7 8 0 | 1 5 4 | 0 9 2

0 4 0 | 3 6 0 | 1 8 0

-------±------±------

0 6 4 | 0 2 3 | 0 5 0

9 0 5 | 0 1 0 | 0 0 0

7 0 0 | 5 9 0 | 0 0 0

solving…

2 3 9 | 4 7 1 | 6 8 5

5 4 8 | 2 6 9 | 3 7 1

6 7 1 | 8 5 3 | 4 2 9

-------±------±------

5 1 6 | 8 9 2 | 3 4 7

7 8 3 | 1 5 4 | 6 9 2

9 4 2 | 3 6 7 | 1 8 5

-------±------±------

1 6 4 | 7 2 3 | 9 5 8

9 2 5 | 8 1 6 | 4 3 7

7 3 8 | 5 9 4 | 2 1 6

Program exited with code 0