brewersfan1976

sudoku2.rb

Feb 16th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.75 KB | None | 0 0
  1. def sudoku2(grid)
  2.    
  3.     x = 0
  4.     y = 0
  5.     occurences = 0
  6.    
  7.     # test for each row
  8.    
  9.     while (x < 9)
  10.        while (y < 9)
  11.              if grid[x][y] == '1' then
  12.                 occurences = occurences + 1
  13.              end
  14.                  
  15.              y = y + 1
  16.        end
  17.        
  18.        if occurences > 1 then
  19.           return false
  20.        end
  21.            
  22.        y = 0
  23.        occurences = 0
  24.        x = x + 1
  25.     end
  26.            
  27.     x = 0
  28.     y = 0
  29.     occurences = 0
  30.    
  31.     while (x < 9)
  32.        while (y < 9)
  33.              if grid[x][y] == '2' then
  34.                 occurences = occurences + 1
  35.              end
  36.                  
  37.              y = y + 1
  38.        end
  39.        
  40.        if occurences > 1 then
  41.           return false
  42.        end
  43.            
  44.        y = 0
  45.        occurences = 0
  46.        x = x + 1
  47.     end
  48.            
  49.     x = 0
  50.     y = 0
  51.     occurences = 0
  52.            
  53.      while (x < 9)
  54.        while (y < 9)
  55.              if grid[x][y] == '3' then
  56.                 occurences = occurences + 1
  57.              end
  58.                  
  59.              y = y + 1
  60.        end
  61.        
  62.        if occurences > 1 then
  63.           return false
  64.        end
  65.            
  66.        y = 0
  67.        occurences = 0
  68.        x = x + 1
  69.     end
  70.            
  71.     x = 0
  72.     y = 0
  73.     occurences = 0
  74.            
  75.      while (x < 9)
  76.        while (y < 9)
  77.              if grid[x][y] == '4' then
  78.                 occurences = occurences + 1
  79.              end
  80.                  
  81.              y = y + 1
  82.        end
  83.        
  84.        if occurences > 1 then
  85.           return false
  86.        end
  87.            
  88.        y = 0
  89.        occurences = 0
  90.        x = x + 1
  91.     end
  92.            
  93.     x = 0
  94.     y = 0
  95.     occurences = 0
  96.            
  97.     while (x < 9)
  98.        while (y < 9)
  99.              if grid[x][y] == '5' then
  100.                 occurences = occurences + 1
  101.              end
  102.                  
  103.              y = y + 1
  104.        end
  105.        
  106.        if occurences > 1 then
  107.           return false
  108.        end
  109.            
  110.        y = 0
  111.        occurences = 0
  112.        x = x + 1
  113.     end
  114.            
  115.     x = 0
  116.     y = 0
  117.     occurences = 0
  118.            
  119.     while (x < 9)
  120.        while (y < 9)
  121.              if grid[x][y] == '6' then
  122.                 occurences = occurences + 1
  123.              end
  124.                  
  125.              y = y + 1
  126.        end
  127.        
  128.        if occurences > 1 then
  129.           return false
  130.        end
  131.            
  132.        y = 0
  133.        occurences = 0
  134.        x = x + 1
  135.     end
  136.            
  137.     x = 0
  138.     y = 0
  139.     occurences = 0
  140.            
  141.     while (x < 9)
  142.        while (y < 9)
  143.              if grid[x][y] == '7' then
  144.                 occurences = occurences + 1
  145.              end
  146.                  
  147.              y = y + 1
  148.        end
  149.        
  150.        if occurences > 1 then
  151.           return false
  152.        end
  153.            
  154.        y = 0
  155.        occurences = 0
  156.        x = x + 1
  157.     end
  158.            
  159.     x = 0
  160.     y = 0
  161.     occurences = 0
  162.            
  163.     while (x < 9)
  164.        while (y < 9)
  165.              if grid[x][y] == '8' then
  166.                 occurences = occurences + 1
  167.              end
  168.                  
  169.              y = y + 1
  170.        end
  171.        
  172.        if occurences > 1 then
  173.           return false
  174.        end
  175.            
  176.        y = 0
  177.        occurences = 0
  178.        x = x + 1
  179.     end
  180.            
  181.     x = 0
  182.     y = 0
  183.     occurences = 0
  184.            
  185.     while (x < 9)
  186.        while (y < 9)
  187.              if grid[x][y] == '9' then
  188.                 occurences = occurences + 1
  189.              end
  190.                  
  191.              y = y + 1
  192.        end
  193.        
  194.        if occurences > 1 then
  195.           return false
  196.        end
  197.            
  198.        y = 0
  199.        occurences = 0
  200.        x = x + 1
  201.     end
  202.            
  203.     # test for each column
  204.    
  205.     x = 0
  206.     y = 0
  207.     occurences = 0
  208.            
  209.     while (y < 9)
  210.        while (x < 9)
  211.              if grid[x][y] == '1' then
  212.                 occurences = occurences + 1
  213.              end
  214.                  
  215.              x = x + 1
  216.        end
  217.        
  218.        if occurences > 1 then
  219.           return false
  220.        end
  221.            
  222.        x = 0
  223.        occurences = 0
  224.        y = y + 1
  225.     end
  226.    
  227.     x = 0
  228.     y = 0
  229.     occurences = 0
  230.            
  231.     while (y < 9)
  232.        while (x < 9)
  233.              if grid[x][y] == '2' then
  234.                 occurences = occurences + 1
  235.              end
  236.                  
  237.              x = x + 1
  238.        end
  239.        
  240.        if occurences > 1 then
  241.           return false
  242.        end
  243.            
  244.        x = 0
  245.        occurences = 0
  246.        y = y + 1
  247.     end
  248.            
  249.      x = 0
  250.     y = 0
  251.     occurences = 0
  252.            
  253.     while (y < 9)
  254.        while (x < 9)
  255.              if grid[x][y] == '3' then
  256.                 occurences = occurences + 1
  257.              end
  258.                  
  259.              x = x + 1
  260.        end
  261.        
  262.        if occurences > 1 then
  263.           return false
  264.        end
  265.            
  266.        x = 0
  267.        occurences = 0
  268.        y = y + 1
  269.     end
  270.      
  271.     x = 0
  272.     y = 0
  273.     occurences = 0
  274.            
  275.      while (y < 9)
  276.        while (x < 9)
  277.              if grid[x][y] == '4' then
  278.                 occurences = occurences + 1
  279.              end
  280.                  
  281.              x = x + 1
  282.        end
  283.        
  284.        if occurences > 1 then
  285.           return false
  286.        end
  287.            
  288.        x = 0
  289.        occurences = 0
  290.        y = y + 1
  291.     end
  292.      
  293.       x = 0
  294.       y = 0
  295.       occurences = 0
  296.            
  297.       while (y < 9)
  298.        while (x < 9)
  299.              if grid[x][y] == '5' then
  300.                 occurences = occurences + 1
  301.              end
  302.                  
  303.              x = x + 1
  304.        end
  305.        
  306.        if occurences > 1 then
  307.           return false
  308.        end
  309.            
  310.        x = 0
  311.        occurences = 0
  312.        y = y + 1
  313.     end          
  314.            
  315.      x = 0
  316.      y = 0
  317.      occurences = 0
  318.            
  319.       while (y < 9)
  320.        while (x < 9)
  321.              if grid[x][y] == '6' then
  322.                 occurences = occurences + 1
  323.              end
  324.                  
  325.              x = x + 1
  326.        end
  327.        
  328.        if occurences > 1 then
  329.           return false
  330.        end
  331.            
  332.        x = 0
  333.        occurences = 0
  334.        y = y + 1
  335.     end    
  336.            
  337.      x = 0
  338.      y = 0
  339.      occurences = 0
  340.            
  341.       while (y < 9)
  342.        while (x < 9)
  343.              if grid[x][y] == '7' then
  344.                 occurences = occurences + 1
  345.              end
  346.                  
  347.              x = x + 1
  348.        end
  349.        
  350.        if occurences > 1 then
  351.           return false
  352.        end
  353.            
  354.        x = 0
  355.        occurences = 0
  356.        y = y + 1
  357.     end                
  358.            
  359.     x = 0
  360.     y = 0
  361.     occurences = 0
  362.            
  363.       while (y < 9)
  364.        while (x < 9)
  365.              if grid[x][y] == '8' then
  366.                 occurences = occurences + 1
  367.              end
  368.                  
  369.              x = x + 1
  370.        end
  371.        
  372.        if occurences > 1 then
  373.           return false
  374.        end
  375.            
  376.        x = 0
  377.        occurences = 0
  378.        y = y + 1
  379.     end                        
  380.    
  381.     x = 0
  382.     y = 0
  383.     occurences = 0
  384.            
  385.       while (y < 9)
  386.        while (x < 9)
  387.              if grid[x][y] == '9' then
  388.                 occurences = occurences + 1
  389.              end
  390.                  
  391.              x = x + 1
  392.        end
  393.        
  394.        if occurences > 1 then
  395.           return false
  396.        end
  397.            
  398.        x = 0
  399.        occurences = 0
  400.        y = y + 1
  401.     end                    
  402.      
  403.     # test 3x3 subgrids
  404.    
  405.     new_array = Array.new
  406.            
  407.     new_array = [grid[0][0], grid[0][1], grid[0][2],
  408.                  grid[1][0], grid[1][1], grid[1][2],
  409.                  grid[2][0], grid[2][1], grid[2][2]]
  410.            
  411.     if new_array.count('1') > 1 then
  412.        return false
  413.     end
  414.        
  415.     if new_array.count('2') > 1 then
  416.        return false
  417.     end
  418.        
  419.     if new_array.count('3') > 1 then
  420.        return false
  421.     end
  422.        
  423.     if new_array.count('4') > 1 then
  424.        return false
  425.     end
  426.        
  427.     if new_array.count('5') > 1 then
  428.        return false
  429.     end
  430.        
  431.     if new_array.count('6') > 1 then
  432.        return false
  433.     end
  434.        
  435.     if new_array.count('7') > 1 then
  436.        return false
  437.     end
  438.        
  439.     if new_array.count('8') > 1 then
  440.        return false
  441.     end
  442.        
  443.     if new_array.count('9') > 1 then
  444.        return false
  445.     end
  446.        
  447.     new_array = Array.new
  448.            
  449.     new_array = [grid[0][3], grid[0][4], grid[0][5],
  450.                  grid[1][3], grid[1][4], grid[1][5],
  451.                  grid[2][3], grid[2][4], grid[2][5]]
  452.            
  453.     if new_array.count('1') > 1 then
  454.        return false
  455.     end
  456.        
  457.     if new_array.count('2') > 1 then
  458.        return false
  459.     end
  460.        
  461.     if new_array.count('3') > 1 then
  462.        return false
  463.     end
  464.        
  465.     if new_array.count('4') > 1 then
  466.        return false
  467.     end
  468.        
  469.     if new_array.count('5') > 1 then
  470.        return false
  471.     end
  472.        
  473.     if new_array.count('6') > 1 then
  474.        return false
  475.     end
  476.        
  477.     if new_array.count('7') > 1 then
  478.        return false
  479.     end
  480.        
  481.     if new_array.count('8') > 1 then
  482.        return false
  483.     end
  484.        
  485.     if new_array.count('9') > 1 then
  486.        return false
  487.     end
  488.    
  489.     new_array = Array.new
  490.            
  491.     new_array = [grid[0][6], grid[0][7], grid[0][8],
  492.                  grid[1][6], grid[1][7], grid[1][8],
  493.                  grid[2][6], grid[2][7], grid[2][8]]
  494.            
  495.     if new_array.count('1') > 1 then
  496.        return false
  497.     end
  498.        
  499.     if new_array.count('2') > 1 then
  500.        return false
  501.     end
  502.        
  503.     if new_array.count('3') > 1 then
  504.        return false
  505.     end
  506.        
  507.     if new_array.count('4') > 1 then
  508.        return false
  509.     end
  510.        
  511.     if new_array.count('5') > 1 then
  512.        return false
  513.     end
  514.        
  515.     if new_array.count('6') > 1 then
  516.        return false
  517.     end
  518.        
  519.     if new_array.count('7') > 1 then
  520.        return false
  521.     end
  522.        
  523.     if new_array.count('8') > 1 then
  524.        return false
  525.     end
  526.        
  527.     if new_array.count('9') > 1 then
  528.        return false
  529.     end
  530.        
  531.     new_array = Array.new
  532.            
  533.     new_array = [grid[3][0], grid[3][1], grid[3][2],
  534.                  grid[4][0], grid[4][1], grid[4][2],
  535.                  grid[5][0], grid[5][1], grid[5][2]]
  536.            
  537.     if new_array.count('1') > 1 then
  538.        return false
  539.     end
  540.        
  541.     if new_array.count('2') > 1 then
  542.        return false
  543.     end
  544.        
  545.     if new_array.count('3') > 1 then
  546.        return false
  547.     end
  548.        
  549.     if new_array.count('4') > 1 then
  550.        return false
  551.     end
  552.        
  553.     if new_array.count('5') > 1 then
  554.        return false
  555.     end
  556.        
  557.     if new_array.count('6') > 1 then
  558.        return false
  559.     end
  560.        
  561.     if new_array.count('7') > 1 then
  562.        return false
  563.     end
  564.        
  565.     if new_array.count('8') > 1 then
  566.        return false
  567.     end
  568.        
  569.     if new_array.count('9') > 1 then
  570.        return false
  571.     end    
  572.    
  573.     new_array = Array.new
  574.            
  575.     new_array = [grid[3][3], grid[3][4], grid[3][5],
  576.                  grid[4][3], grid[4][4], grid[4][5],
  577.                  grid[5][3], grid[5][4], grid[5][5]]
  578.            
  579.     if new_array.count('1') > 1 then
  580.        return false
  581.     end
  582.        
  583.     if new_array.count('2') > 1 then
  584.        return false
  585.     end
  586.        
  587.     if new_array.count('3') > 1 then
  588.        return false
  589.     end
  590.        
  591.     if new_array.count('4') > 1 then
  592.        return false
  593.     end
  594.        
  595.     if new_array.count('5') > 1 then
  596.        return false
  597.     end
  598.        
  599.     if new_array.count('6') > 1 then
  600.        return false
  601.     end
  602.        
  603.     if new_array.count('7') > 1 then
  604.        return false
  605.     end
  606.        
  607.     if new_array.count('8') > 1 then
  608.        return false
  609.     end
  610.        
  611.     if new_array.count('9') > 1 then
  612.        return false
  613.     end
  614.        
  615.     new_array = Array.new
  616.        
  617.     new_array = [grid[3][6], grid[3][7], grid[3][8],
  618.                  grid[4][6], grid[4][7], grid[4][8],
  619.                  grid[5][6], grid[5][7], grid[5][8]]
  620.            
  621.     if new_array.count('1') > 1 then
  622.        return false
  623.     end
  624.        
  625.     if new_array.count('2') > 1 then
  626.        return false
  627.     end
  628.        
  629.     if new_array.count('3') > 1 then
  630.        return false
  631.     end
  632.        
  633.     if new_array.count('4') > 1 then
  634.        return false
  635.     end
  636.        
  637.     if new_array.count('5') > 1 then
  638.        return false
  639.     end
  640.        
  641.     if new_array.count('6') > 1 then
  642.        return false
  643.     end
  644.        
  645.     if new_array.count('7') > 1 then
  646.        return false
  647.     end
  648.        
  649.     if new_array.count('8') > 1 then
  650.        return false
  651.     end
  652.        
  653.     if new_array.count('9') > 1 then
  654.        return false
  655.     end        
  656.        
  657.     new_array = Array.new
  658.        
  659.     new_array = [grid[6][0], grid[6][1], grid[6][2],
  660.                  grid[7][0], grid[7][1], grid[7][2],
  661.                  grid[8][0], grid[8][1], grid[8][2]]
  662.            
  663.     if new_array.count('1') > 1 then
  664.        return false
  665.     end
  666.        
  667.     if new_array.count('2') > 1 then
  668.        return false
  669.     end
  670.        
  671.     if new_array.count('3') > 1 then
  672.        return false
  673.     end
  674.        
  675.     if new_array.count('4') > 1 then
  676.        return false
  677.     end
  678.        
  679.     if new_array.count('5') > 1 then
  680.        return false
  681.     end
  682.        
  683.     if new_array.count('6') > 1 then
  684.        return false
  685.     end
  686.        
  687.     if new_array.count('7') > 1 then
  688.        return false
  689.     end
  690.        
  691.     if new_array.count('8') > 1 then
  692.        return false
  693.     end
  694.        
  695.     if new_array.count('9') > 1 then
  696.        return false
  697.     end            
  698.        
  699.     new_array = Array.new
  700.        
  701.     new_array = [grid[6][3], grid[6][4], grid[6][5],
  702.                  grid[7][3], grid[7][4], grid[7][5],
  703.                  grid[8][3], grid[8][4], grid[8][5]]
  704.            
  705.     if new_array.count('1') > 1 then
  706.        return false
  707.     end
  708.        
  709.     if new_array.count('2') > 1 then
  710.        return false
  711.     end
  712.        
  713.     if new_array.count('3') > 1 then
  714.        return false
  715.     end
  716.        
  717.     if new_array.count('4') > 1 then
  718.        return false
  719.     end
  720.        
  721.     if new_array.count('5') > 1 then
  722.        return false
  723.     end
  724.        
  725.     if new_array.count('6') > 1 then
  726.        return false
  727.     end
  728.        
  729.     if new_array.count('7') > 1 then
  730.        return false
  731.     end
  732.        
  733.     if new_array.count('8') > 1 then
  734.        return false
  735.     end
  736.        
  737.     if new_array.count('9') > 1 then
  738.        return false
  739.     end              
  740.      
  741.     new_array = Array.new
  742.        
  743.     new_array = [grid[6][6], grid[6][7], grid[6][8],
  744.                  grid[7][6], grid[7][7], grid[7][8],
  745.                  grid[8][6], grid[8][7], grid[8][8]]
  746.            
  747.     if new_array.count('1') > 1 then
  748.        return false
  749.     end
  750.        
  751.     if new_array.count('2') > 1 then
  752.        return false
  753.     end
  754.        
  755.     if new_array.count('3') > 1 then
  756.        return false
  757.     end
  758.        
  759.     if new_array.count('4') > 1 then
  760.        return false
  761.     end
  762.        
  763.     if new_array.count('5') > 1 then
  764.        return false
  765.     end
  766.        
  767.     if new_array.count('6') > 1 then
  768.        return false
  769.     end
  770.        
  771.     if new_array.count('7') > 1 then
  772.        return false
  773.     end
  774.        
  775.     if new_array.count('8') > 1 then
  776.        return false
  777.     end
  778.        
  779.     if new_array.count('9') > 1 then
  780.        return false
  781.     end              
  782.        
  783.     return true
  784. end
Advertisement
Add Comment
Please, Sign In to add comment