# Fwd: Sudoku

**URL:** <https://www.ruby-forum.com/t/fwd-sudoku/82891>\
**Category:** Ruby\
**Created:** [December 30, 2005, 3:17pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891 "2005-12-30T15:17:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 3:17pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/1 "2005-12-30T15:17:37Z")

</div>

Begin forwarded message:

> From: NILSSON Christer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Date: December 30, 2005 4:09:59 AM CST  
> To: James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Subject: Re: Ruby Q. Suggestion  
> Reply-To: NILSSON Christer [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> 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]

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 4:18pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/2 "2005-12-30T16:18:28Z")

</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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 5:20pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/3 "2005-12-30T17:20:07Z")

</div>

Craig D. 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:  
s[rowMajor][colMajor][x][y], x in 0…2, y in 0…2  
Fixing rowMajor and rowMinor gives a row  
s[rowMajor][x][rowMinor][y]  
Fixing colMajor and colMinor gives a column  
s[x][colMajor][y][colMinor]

I hope this clarifies the code.

Christer

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 8:17pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/4 "2005-12-30T20:17:33Z")

</div>

2005/12/30, Craig D. removed\_email\_address@domain.invalid:

> 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:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 8:31pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/5 "2005-12-30T20:31:34Z")

</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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 8:29pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/6 "2005-12-30T20:29:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 8:32pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/7 "2005-12-30T20:32:50Z")

</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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 8:50pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/8 "2005-12-30T20:50:51Z")

</div>

Christer N. wrote:

> 2005, All Rights Reserved  
> for b in 0…2  
> end  
> @s[a][b][c][d] = digit  
> return true if @s[a][b][x][y]==digit # block  
> [[5,0,0],[0,6,9],[0,7,1]],  
> if sudoku.solve(0,0,0,0) then  
> 0 4 0 | 3 6 0 | 1 8 0  
> -------±------±------  
> 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:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 9:27pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/9 "2005-12-30T21:27:35Z")

</div>

> First Column has two 9’s !

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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 9:38pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/10 "2005-12-30T21:38:08Z")

</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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 9:40pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/11 "2005-12-30T21:40:37Z")

</div>

Dirk M. wrote:

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

Would you mind sending me your input, Dirk?

Christer

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [December 30, 2005, 10:21pm UTC](https://www.ruby-forum.com/t/fwd-sudoku/82891/12 "2005-12-30T22:21:55Z")

</div>

Christer N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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.
