# \[SOLUTION\] Sudoku

**URL:** https://rubytalk.org/t/solution-sudoku/23688
**Category:** ruby-talk
**Created:** [30 December 2005 14:17 UTC](https://rubytalk.org/t/solution-sudoku/23688 "2005-12-30T14:17:12Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)
#### Post date: [30 December 2005 14:17 UTC](https://rubytalk.org/t/solution-sudoku/23688/1 "2005-12-30T14:17:12Z")

</div>

> From: NILSSON Christer \<christer.nilsson@nordicsolution.com\>  
> Date: December 30, 2005 4:09:59 AM CST  
> To: James Edward Gray II \<james@grayproductions.net\>  
> Subject: Re: Ruby Quiz Suggestion  
> Reply-To: NILSSON Christer \<christer.nilsson@nordicsolution.com\>
> 
> Hello James!

[snip]

> I found a short solution for Sudoku. It was originally written by Kevin Greer in JavaScript, but Ruby makes it look even nicer. I haven't seen the use of this data structure in Sudoku before.

[snip]

> **···**
>
> Begin forwarded message:
> 
> > regards  
> > Christer
> > 
> > class Sudoku  
> > &nbsp;&nbsp;def initialize s  
> > &nbsp;&nbsp;&nbsp;&nbsp;@s=s  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def solve a, b, c, d  
> > &nbsp;&nbsp;&nbsp;&nbsp;return true if a==3  
> > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a+1, 0, c, d) if b==3  
> > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b+1, 0, d) if c==3  
> > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c+1, 0) if d==3  
> > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c, d+1) if @s[a][b][c][d]!=0
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;for digit in 1..9  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not occupied?(a, b, c, d, digit) then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@s[a][b][c][d] = digit  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if solve(a, b, c, d+1)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@s[a][b][c][d] = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;false  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def occupied? a, b, c, d, digit  
> > &nbsp;&nbsp;&nbsp;&nbsp;for x in 0..2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for y in 0..2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][b][y]==digit # block  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][c][y]==digit # row  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[b][y][d]==digit # column  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;false  
> > &nbsp;&nbsp;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]]],  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[[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]]],  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[[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.solve(0,0,0,0)

---

<div class="post-metadata">

### Author: ![Craig\_Demyanovich1](https://avatars.discourse-cdn.com/v4/letter/c/8797f3/32.png) [@Craig\_Demyanovich1](https://rubytalk.org/u/Craig_Demyanovich1)
#### Post date: [30 December 2005 15:17 UTC](https://rubytalk.org/t/solution-sudoku/23688/2 "2005-12-30T15:17:15Z")

</div>

Greetings,

While it may be true that the Sudoku program below looks nicer in Ruby than JavaScript, I don't find it very readable. Sure, it's concise, but that doesn't help me understand it. The variable names are horrible. As I learn Ruby, I'm seeing more and more of this poor naming, favoring conciseness so much over clarity that some code samples are outright frustrating to read for this "nuby." I hope that I'm just stumbling on some bad examples and not the norm. I hope that the more Ruby code I read, the more I'll find a pragmatic compromise between conciseness and clarity.

Regards,  
Craig

> **···**
>
> On Dec 30, 2005, at 9:17 AM, James Edward Gray II wrote:
> 
> > Begin forwarded message:
> > 
> > > From: NILSSON Christer \<christer.nilsson@nordicsolution.com\>  
> > > Date: December 30, 2005 4:09:59 AM CST  
> > > To: James Edward Gray II \<james@grayproductions.net\>  
> > > Subject: Re: Ruby Quiz Suggestion  
> > > Reply-To: NILSSON Christer \<christer.nilsson@nordicsolution.com\>
> > > 
> > > Hello James!
> > 
> > [snip]
> > 
> > > I found a short solution for Sudoku. It was originally written by Kevin Greer in JavaScript, but Ruby makes it look even nicer. I haven't seen the use of this data structure in Sudoku before.
> > 
> > [snip]
> > 
> > > regards  
> > > Christer
> > > 
> > > class Sudoku  
> > > &nbsp;&nbsp;def initialize s  
> > > &nbsp;&nbsp;&nbsp;&nbsp;@s=s  
> > > &nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;def solve a, b, c, d  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return true if a==3  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a+1, 0, c, d) if b==3  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b+1, 0, d) if c==3  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c+1, 0) if d==3  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c, d+1) if @s[a][b][c][d]!=0
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;for digit in 1..9  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not occupied?(a, b, c, d, digit) then  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@s[a][b][c][d] = digit  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if solve(a, b, c, d+1)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@s[a][b][c][d] = 0  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;false  
> > > &nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;def occupied? a, b, c, d, digit  
> > > &nbsp;&nbsp;&nbsp;&nbsp;for x in 0..2  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for y in 0..2  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][b][y]==digit # block  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][c][y]==digit # row  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[b][y][d]==digit # column  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;false  
> > > &nbsp;&nbsp;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]]],  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[[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]]],  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[[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.solve(0,0,0,0)

---

<div class="post-metadata">

### Author: ![Christer\_Nilsson](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@Christer\_Nilsson](https://rubytalk.org/u/Christer_Nilsson)
#### Post date: [30 December 2005 16:20 UTC](https://rubytalk.org/t/solution-sudoku/23688/3 "2005-12-30T16:20:29Z")

</div>

Craig Demyanovich wrote:

> The variable names are horrible.

Yes, you are correct. This is puzzle code, not production code. So part  
of this is being puzzled! 🙂

Joke aside, I considered using better variable names instead of a,b,c  
and d, but did not find good candidates. Let me explain:

a, c = row.divmod(3) # row in 0..8  
b, d = col.divmod(3) # col in 0..8

Maybe a=rowMajor, c=rowMinor, b=colMajor and d=colMinor would be more  
helpful.

Fixing rowMajor and colMajor gives a 3x3 block:  
&nbsp;&nbsp;s[rowMajor][colMajor][y], x in 0..2, y in 0..2  
Fixing rowMajor and rowMinor gives a row  
&nbsp;&nbsp;s[rowMajor][rowMinor][y]  
Fixing colMajor and colMinor gives a column  
&nbsp;&nbsp;s[colMajor][y][colMinor]

I hope this clarifies the code.

Christer

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Dirk\_Meijer](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@Dirk\_Meijer](https://rubytalk.org/u/Dirk_Meijer)
#### Post date: [30 December 2005 19:16 UTC](https://rubytalk.org/t/solution-sudoku/23688/4 "2005-12-30T19:16:57Z")

</div>

2005/12/30, Craig Demyanovich demmer12@fastmail.us:

> While it may be true that the Sudoku program below looks nicer in  
> Ruby than JavaScript, I don't find it very readable. Sure, it's  
> concise, but that doesn't help me understand it.

i wouldn't call it concise, it simply doesn't work.. probably an error in  
porting to Ruby..  
the idea of simply going through every number and checking possibility is  
nice, but not enough, because a lot of numbers might fit, though only one  
will get you the proper solution.

---

<div class="post-metadata">

### Author: ![Christer\_Nilsson](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@Christer\_Nilsson](https://rubytalk.org/u/Christer_Nilsson)
#### Post date: [30 December 2005 19:29 UTC](https://rubytalk.org/t/solution-sudoku/23688/5 "2005-12-30T19:29:17Z")

</div>

Dirk Meijer 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 Nilsson 2005-12-29

class Sudoku

&nbsp;&nbsp;def initialize s  
&nbsp;&nbsp;&nbsp;&nbsp;@s=s  
&nbsp;&nbsp;end

&nbsp;&nbsp;def display  
&nbsp;&nbsp;&nbsp;&nbsp;for a in 0..2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for b in 0..2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for c in 0..2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for d in 0..2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print " " + @s[a][b][c][d].to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print " |" if c\<2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\n"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "-------+-------+-------\n" if a\<2  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def solve a, b, c, d  
&nbsp;&nbsp;&nbsp;&nbsp;return true if a==3  
&nbsp;&nbsp;&nbsp;&nbsp;return solve(a+1, 0, c, d) if b==3  
&nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b+1, 0, d) if c==3  
&nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c+1, 0) if d==3  
&nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c, d+1) if @s[a][b][c][d]!=0

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

&nbsp;&nbsp;def occupied? a, b, c, d, digit  
&nbsp;&nbsp;&nbsp;&nbsp;for x in 0..2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for y in 0..2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][b][y]==digit # block  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][c][y]==digit # row  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[b][y][d]==digit # column  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;false  
&nbsp;&nbsp;end  
end

s =  
[[[[0,0,0],[0,7,1],[0,0,5]],  
&nbsp;&nbsp;&nbsp;[[5,0,0],[0,6,9],[0,7,1]],  
&nbsp;&nbsp;&nbsp;[[0,7,1],[8,5,3],[4,2,0]]],  
&nbsp;&nbsp;[[[0,1,0],[0,0,2],[0,0,0]],  
&nbsp;&nbsp;&nbsp;[[7,8,0],[1,5,4],[0,9,2]],  
&nbsp;&nbsp;&nbsp;[[0,4,0],[3,6,0],[1,8,0]]],  
&nbsp;&nbsp;[[[0,6,4],[0,2,3],[0,5,0]],  
&nbsp;&nbsp;&nbsp;[[9,0,5],[0,1,0],[0,0,0]],  
&nbsp;&nbsp;&nbsp;[[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  
&nbsp;&nbsp;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
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Christer\_Nilsson](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@Christer\_Nilsson](https://rubytalk.org/u/Christer_Nilsson)
#### Post date: [30 December 2005 19:31 UTC](https://rubytalk.org/t/solution-sudoku/23688/6 "2005-12-30T19:31:46Z")

</div>

Link to the original Javascript code by Kevin Greer:

[http://www.peerbox.com:8668/space/start/2005-12-5/1#Soduko\_Solver\_in\_JavaScript](http://www.peerbox.com:8668/space/start/2005-12-5/1#Soduko_Solver_in_JavaScript)

Christer

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Simon\_Kroger](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Simon\_Kroger](https://rubytalk.org/u/Simon_Kroger)
#### Post date: [30 December 2005 19:49 UTC](https://rubytalk.org/t/solution-sudoku/23688/7 "2005-12-30T19:49:36Z")

</div>

Christer Nilsson wrote:

> Dirk Meijer 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 Nilsson 2005-12-29
> 
> class Sudoku
> 
> &nbsp;&nbsp;def initialize s  
> &nbsp;&nbsp;&nbsp;&nbsp;@s=s  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def display  
> &nbsp;&nbsp;&nbsp;&nbsp;for a in 0..2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for b in 0..2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for c in 0..2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for d in 0..2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print " " + @s[a][b][c][d].to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print " |" if c\<2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\n"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "-------+-------+-------\n" if a\<2  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def solve a, b, c, d  
> &nbsp;&nbsp;&nbsp;&nbsp;return true if a==3  
> &nbsp;&nbsp;&nbsp;&nbsp;return solve(a+1, 0, c, d) if b==3  
> &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b+1, 0, d) if c==3  
> &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c+1, 0) if d==3  
> &nbsp;&nbsp;&nbsp;&nbsp;return solve(a, b, c, d+1) if @s[a][b][c][d]!=0
> 
> &nbsp;&nbsp;&nbsp;&nbsp;for digit in 1..9  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not occupied?(a, b, c, d, digit) then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@s[a][b][c][d] = digit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if solve(a, b, c, d+1)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@s[a][b][c][d] = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;false  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def occupied? a, b, c, d, digit  
> &nbsp;&nbsp;&nbsp;&nbsp;for x in 0..2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for y in 0..2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][b][y]==digit # block  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[a][c][y]==digit # row  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true if @s[b][y][d]==digit # column  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;false  
> &nbsp;&nbsp;end  
> end
> 
> s =  
> [[[[0,0,0],[0,7,1],[0,0,5]],  
> &nbsp;&nbsp;&nbsp;[[5,0,0],[0,6,9],[0,7,1]],  
> &nbsp;&nbsp;&nbsp;[[0,7,1],[8,5,3],[4,2,0]]],  
> &nbsp;&nbsp;[[[0,1,0],[0,0,2],[0,0,0]],  
> &nbsp;&nbsp;&nbsp;[[7,8,0],[1,5,4],[0,9,2]],  
> &nbsp;&nbsp;&nbsp;[[0,4,0],[3,6,0],[1,8,0]]],  
> &nbsp;&nbsp;[[[0,6,4],[0,2,3],[0,5,0]],  
> &nbsp;&nbsp;&nbsp;[[9,0,5],[0,1,0],[0,0,0]],  
> &nbsp;&nbsp;&nbsp;[[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  
> &nbsp;&nbsp;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

First Column has two 9's !

cheers

Simon

---

<div class="post-metadata">

### Author: ![Christer\_Nilsson](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@Christer\_Nilsson](https://rubytalk.org/u/Christer_Nilsson)
#### Post date: [30 December 2005 19:33 UTC](https://rubytalk.org/t/solution-sudoku/23688/8 "2005-12-30T19:33:02Z")

</div>

Correct link:

[http://www.peerbox.com:8668/space/start/2005-12-25/1#Soduko\_Solver\_in\_JavaScript](http://www.peerbox.com:8668/space/start/2005-12-25/1#Soduko_Solver_in_JavaScript)

cheers

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Dirk\_Meijer](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@Dirk\_Meijer](https://rubytalk.org/u/Dirk_Meijer)
#### Post date: [30 December 2005 20:25 UTC](https://rubytalk.org/t/solution-sudoku/23688/9 "2005-12-30T20:25:37Z")

</div>

> First Column has two 9's !

and it leaves my tests with zero's..  
greetings, Dirk.

---

<div class="post-metadata">

### Author: ![Christer\_Nilsson](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@Christer\_Nilsson](https://rubytalk.org/u/Christer_Nilsson)
#### Post date: [30 December 2005 20:38 UTC](https://rubytalk.org/t/solution-sudoku/23688/10 "2005-12-30T20:38:20Z")

</div>

> First Column has two 9's !  
> cheers  
> Simon

Excellent observation, Simon!

The error is in the display definition:  
- print " " + @s[a][b][c][d].to\_s  
+ print " " + @s[a][c][b][d].to\_s

New output:

2 3 9 | 5 4 8 | 6 7 1  
4 7 1 | 2 6 9 | 8 5 3  
6 8 5 | 3 7 1 | 4 2 9

> **···**
>
> -------+-------+-------  
> 5 1 6 | 7 8 3 | 9 4 2  
> 8 9 2 | 1 5 4 | 3 6 7  
> 3 4 7 | 6 9 2 | 1 8 5  
> -------+-------+-------  
> 1 6 4 | 9 2 5 | 7 3 8  
> 7 2 3 | 8 1 6 | 5 9 4  
> 9 5 8 | 4 3 7 | 2 1 6
> 
> Sorry, I shouldn't have opened that bottle of champagne yet!
> 
> Christer
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Christer\_Nilsson](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@Christer\_Nilsson](https://rubytalk.org/u/Christer_Nilsson)
#### Post date: [30 December 2005 20:40 UTC](https://rubytalk.org/t/solution-sudoku/23688/11 "2005-12-30T20:40:50Z")

</div>

Dirk Meijer wrote:

> and it leaves my tests with zero's..  
> greetings, Dirk.

Would you mind sending me your input, Dirk?

Christer

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Dirk\_Meijer](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@Dirk\_Meijer](https://rubytalk.org/u/Dirk_Meijer)
#### Post date: [30 December 2005 21:19 UTC](https://rubytalk.org/t/solution-sudoku/23688/12 "2005-12-30T21:19:16Z")

</div>

Christer Nilsson \<janchrister.nilsson@gmail.com\>:

> Would you mind sending me your input, Dirk?

it seems this was also a problem with the arrays,  
i had entered the puzzle from left to right, i just found out it had to be  
done one block at the time..  
anyway, sorry for the fuzz 😉  
greetings, Dirk.
