Announcement

Collapse
No announcement yet.

I have written a computer program which solves Sudoku puzzles

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • I have written a computer program which solves Sudoku puzzles

    Point at me and laugh, for I am sad.
    12-17-10 Mohamed Bouazizi NEVER FORGET
    Stadtluft Macht Frei
    Killing it is the new killing it
    Ultima Ratio Regum

  • #2
    Code:
    function square, r, c
    s = 3*floor(r/3.0) + floor(c/3.0)
    return, s
    end
    
    function delta, a, b
    s = 0
    if (a EQ b) then begin
    	s = 1
    endif
    return, s
    end
    
    function eliminator, poss
    a = 0L
    while a LE 100L do begin
    	totalposs0 = total(poss)
    	sumvals = total(poss, 3)
    	for r = 0, 8 do begin
    		for c = 0, 8 do begin
    			if (sumvals[r, c] EQ 0) then begin
    				poss = fltarr(9, 9, 9)
    				a = a + 10000L
    			endif
    			if (sumvals[r, c] EQ 1) then begin
    				for j = 0, 8 do begin
    					if (poss[r, c, j] EQ 1) then begin
    						for rprime = 0, 8 do begin
    							poss[rprime, c, j] = delta(r, rprime)
    						endfor
    						for cprime = 0, 8 do begin
    							poss[r, cprime, j] = delta(c, cprime)
    						endfor
    						for rprime = 0, 8 do begin
    							for cprime = 0, 8 do begin
    								if (square(r, c) EQ square(rprime, cprime)) then begin
    									poss[rprime, cprime, j] = poss[rprime, cprime, j] * delta(r, rprime) * delta(c, cprime)
    								endif
    							endfor
    						endfor
    					endif
    				endfor
    			endif
    		endfor
    	endfor
    	sumrows = total(poss, 1)
    	for c = 0, 8 do begin
    		for j = 0, 8 do begin
    			if (sumrows[c, j] EQ 0) then begin
    				poss = fltarr(9, 9, 9)
    				a = a + 10000L
    			endif
    			if (sumrows[c, j] EQ 1) then begin
    				for r = 0, 8 do begin
    					if (poss[r, c, j] EQ 1) then begin
    						for jprime = 0, 8 do begin
    							poss[r, c, jprime] = delta(j, jprime)
    						endfor
    					endif
    				endfor
    			endif
    		endfor
    	endfor
    	sumcols = total(poss, 2)
    	for r = 0, 8 do begin
    		for j = 0, 8 do begin
    			if (sumcols[r, j] EQ 0) then begin
    				poss = fltarr(9, 9, 9)
    				a = a + 10000L
    			endif
    			if (sumcols[r, j] EQ 1) then begin
    				for c = 0, 8 do begin
    					if (poss[r, c, j] EQ 1) then begin
    						for jprime = 0, 8 do begin
    							poss[r, c, jprime] = delta(j, jprime)
    						endfor
    					endif
    				endfor
    			endif
    		endfor
    	endfor
    	sumsqrs = fltarr(9, 9)
    	for j = 0, 8 do begin
    		for r = 0, 8 do begin
    			for c = 0, 8 do begin
    				sqr = square(r, c)
    				sumsqrs(sqr, j) = sumsqrs(sqr, j) + poss[r, c, j]
    			endfor
    		endfor
    	endfor
    	for sqr = 0, 8 do begin
    		for j = 0, 8 do begin
    			if (sumsqrs[sqr, j] EQ 0) then begin
    				poss = fltarr(9, 9, 9)
    				a = a + 10000L
    			endif
    			if (sumsqrs[sqr, j] EQ 1) then begin
    				for r = 0, 8 do begin
    					for c = 0, 8 do begin
    						if (square(r, c) EQ sqr) then begin
    							if (poss[r, c, j] EQ 1) then begin
    								for jprime = 0, 8 do begin
    									poss[r, c, jprime] = delta(j, jprime)
    								endfor
    							endif
    						endif
    					endfor
    				endfor
    			endif
    		endfor
    	endfor
    	totalposs1 = total(poss)
    	if (totalposs1 EQ totalposs0) then begin
    		a = a + 10000L
    	endif
    	a = a + 1
    endwhile
    return, poss
    end
    
    pro solver
    n = 28
    ;n is the number of values pre-determined by the person who wrote the puzzle
    grow = intarr(n)
    gcol = intarr(n)
    gval = intarr(n)
    ;given values for given row and given column
    ;note that row and column numbering begins at 0, but gval must be an integer from 1 to 9
    grow[0] = 0
    gcol[0] = 0
    gval[0] = 9
    grow[1] = 0
    gcol[1] = 1
    gval[1] = 5
    grow[2] = 0
    gcol[2] = 6
    gval[2] = 6
    grow[3] = 0
    gcol[3] = 7
    gval[3] = 7
    grow[4] = 1
    gcol[4] = 5
    gval[4] = 7
    grow[5] = 1
    gcol[5] = 6
    gval[5] = 3
    grow[6] = 1
    gcol[6] = 7
    gval[6] = 1
    grow[7] = 2
    gcol[7] = 0
    gval[7] = 8
    grow[8] = 2
    gcol[8] = 3
    gval[8] = 5
    grow[9] = 2
    gcol[9] = 4
    gval[9] = 6
    grow[10] = 2
    gcol[10] = 5
    gval[10] = 1
    grow[11] = 3
    gcol[11] = 0
    gval[11] = 2
    grow[12] = 3
    gcol[12] = 1
    gval[12] = 3
    grow[13] = 4
    gcol[13] = 2
    gval[13] = 9
    grow[14] = 4
    gcol[14] = 6
    gval[14] = 1
    grow[15] = 5
    gcol[15] = 7
    gval[15] = 3
    grow[16] = 5
    gcol[16] = 8
    gval[16] = 7
    grow[17] = 6
    gcol[17] = 3
    gval[17] = 1
    grow[18] = 6
    gcol[18] = 4
    gval[18] = 4
    grow[19] = 6
    gcol[19] = 5
    gval[19] = 5
    grow[20] = 6
    gcol[20] = 8
    gval[20] = 9
    grow[21] = 7
    gcol[21] = 1
    gval[21] = 9
    grow[22] = 7
    gcol[22] = 2
    gval[22] = 4
    grow[23] = 7
    gcol[23] = 3
    gval[23] = 2
    grow[24] = 8
    gcol[24] = 1
    gval[24] = 8
    grow[25] = 8
    gcol[25] = 2
    gval[25] = 5
    grow[26] = 8
    gcol[26] = 7
    gval[26] = 2
    grow[27] = 8
    gcol[27] = 8
    gval[27] = 1
    poss = intarr(9, 9, 9)
    for r = 0, 8 do begin
    	for c = 0, 8 do begin
    		for j = 0, 8 do begin
    			poss[r, c, j] = 1
    		endfor
    	endfor
    endfor
    for i = 0, (n - 1) do begin
    	for j = 0, 8 do begin
    		if (j NE (gval[i] - 1)) then begin
    			poss[grow[i], gcol[i], j] = 0
    		endif
    	endfor
    endfor
    poss = eliminator(poss)
    if (total(poss) GT 81) then begin
    	a = 0L
    	b = 0L
    	while (a LE 100L) do begin
    		sumvals = total(poss, 3)
    		possprime = poss
    		r = floor(b / 81.0)
    		c = (floor(b / 9.0)) MOD 9
    		j = b MOD 9
    ;		print, r
    ;		print, c
    ;		print, j
    		if ((sumvals[r, c] GE 2) AND (poss[r, c, j] EQ 1)) then begin
    			print, "made it into if loop"
    			print, r
    			print, c
    			print, (j + 1)
    			for jprime = 0, 8 do begin
    				possprime[r, c, jprime] = delta(j, jprime)
    			endfor
    			possprime = eliminator(possprime)
    			if (total(possprime) EQ 0) then begin
    				print, "Possibility Eliminated!"
    				poss[r, c, j] = 0
    				poss = eliminator(poss)
    			endif
    			a = a + 1L
    		endif
    		if ((total(poss) EQ 81) OR (b GT 729L)) then begin
    			a = a + 10000L
    		endif
    		b = b + 1L
    	endwhile
    endif
    print, "Solution begins"
    for r = 0L, 8L do begin
    	print, "beginning of row"
    	for c = 0L, 8L do begin
    		count = 0L
    		p = 0L
    		for j = 0, 8 do begin
    			p = p + (j + 1) * poss[r, c, j] * (10L^(count))
    			count = count + poss[r, c, j]
    		endfor
    		print, p
    	endfor
    endfor
    end
    Code is in IDL, which is similar to Fortran
    12-17-10 Mohamed Bouazizi NEVER FORGET
    Stadtluft Macht Frei
    Killing it is the new killing it
    Ultima Ratio Regum

    Comment


    • #3
      I could've done that if I knew how to.

      Comment


      • #4
        12-17-10 Mohamed Bouazizi NEVER FORGET
        Stadtluft Macht Frei
        Killing it is the new killing it
        Ultima Ratio Regum

        Comment


        • #5
          those are somtimes sort of fun to do

          i Get bored of them though

          JM
          Jon Miller-
          I AM.CANADIAN
          GENERATION 35: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.

          Comment


          • #6
            You never have to do one again, then.

            Just copy-paste, enter the requisite initial values, compile in IDL and run...

            I only found out about these 2 or 3 days ago.
            12-17-10 Mohamed Bouazizi NEVER FORGET
            Stadtluft Macht Frei
            Killing it is the new killing it
            Ultima Ratio Regum

            Comment


            • #7
              ...well done!
              "I work in IT so I'd be buggered without a computer" - Words of wisdom from Provost Harrison
              "You can be wrong AND jewish" - Wiglaf :love:

              Comment


              • #8
                Jesus... and I thought I needed a girlfriend
                “I give you a new commandment, that you love one another. Just as I have loved you, you also should love one another. By this everyone will know that you are my disciples, if you have love for one another.”
                - John 13:34-35 (NRSV)

                Comment


                • #9
                  You've just knocked out the corner stone of modern British cultural fads.

                  How popular is Soduku in other countries? It's massive in the UK...We've had Soduku gameshows on TV, dozens of puzzle collections released...Even an intereactive DVD game.
                  Exult in your existence, because that very process has blundered unwittingly on its own negation. Only a small, local negation, to be sure: only one species, and only a minority of that species; but there lies hope. [...] Stand tall, Bipedal Ape. The shark may outswim you, the cheetah outrun you, the swift outfly you, the capuchin outclimb you, the elephant outpower you, the redwood outlast you. But you have the biggest gifts of all: the gift of understanding the ruthlessly cruel process that gave us all existence [and the] gift of revulsion against its implications.
                  -Richard Dawkins

                  Comment


                  • #10
                    I know someone who wrote one, too.

                    Perhaps you could have saved some time by contacting him.
                    "You're the biggest user of hindsight that I've ever known. Your favorite team, in any sport, is the one that just won. If you were a woman, you'd likely be a slut." - Slowwhand, to Imran

                    Eschewing silly games since December 4, 2005

                    Comment


                    • #11
                      Originally posted by Starchild
                      How popular is Soduku in other countries? It's massive in the UK...We've had Soduku gameshows on TV, dozens of puzzle collections released...Even an intereactive DVD game.
                      And in contrast, I had to look up the thing on Wikipedia. Though it seems somewhat familiar after reading up on it.
                      “I give you a new commandment, that you love one another. Just as I have loved you, you also should love one another. By this everyone will know that you are my disciples, if you have love for one another.”
                      - John 13:34-35 (NRSV)

                      Comment


                      • #12
                        They got popular in 2005 where I'm from. They're now in newspapers, in the same section as crossword puzzles and the like.
                        "You're the biggest user of hindsight that I've ever known. Your favorite team, in any sport, is the one that just won. If you were a woman, you'd likely be a slut." - Slowwhand, to Imran

                        Eschewing silly games since December 4, 2005

                        Comment


                        • #13
                          Originally posted by Jaguar
                          They got popular in 2005 where I'm from. They're now in newspapers, in the same section as crossword puzzles and the like.
                          QFditto

                          Comment


                          • #14
                            2005

                            Dawn of the Dead Brains
                            I will never understand why some people on Apolyton find you so clever. You're predictable, mundane, and a google-whore and the most observant of us all know this. Your battles of "wits" rely on obscurity and whenever you fail to find something sufficiently obscure, like this, you just act like a 5 year old. Congratulations, molly.

                            Asher on molly bloom

                            Comment


                            • #15
                              From what I can tell they've become popular here; both my parents are hooked on the things.

                              Before christmas, Barnes & Noble had no less than 3 Sudoku displays set up throughout the store with books and calendars of the things.

                              Comment

                              Working...
                              X