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 > def initialize s > @s=s > 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.solve(0,0,0,0) > >