Advertisement
MrMusAddict

Sudoku Solver (Excel VBA Macro)

Aug 5th, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub sud()
  2.  
  3. Dim s9(9, 9) As Integer 'x/y row/col address
  4. Dim row As Integer
  5. Dim col As Integer
  6. Dim rows(9, 9) As Integer '(x,y) to check if y row contain number x
  7. Dim cols(9, 9) As Integer '(x,y) to check if y column contains number x
  8. Dim square(9, 9) As Integer '(x,y) to check if y square contains by number x
  9. Dim onlySquare(9, 9) As Integer '(x,y) used to check if there's only one occurrence of number x in square y
  10. Dim onlyRow(9, 9) As Integer '(x,y) used to check if there's only one occurrence of number x in row y
  11. Dim onlyCol(9, 9) As Integer '(x,y) used to check if there's only one occurrence of number x in column y
  12.  
  13. Dim sqr As Integer
  14. Dim num As Integer
  15.  
  16. Dim osCount As Integer 'Only Square Count, temp value to count the number of occurrences of a number within a square
  17. Dim orCount As Integer 'Only Row Count, temp value to count the number of occurrences of a number within a row
  18. Dim ocCount As Integer 'Only Column Count, temp value to count the number of occurrences of a number within a column
  19.  
  20. Dim iterations As Integer
  21.  
  22. iterations = 50 '' the code is intended to iterate on itself for process of elimination
  23.  
  24. For x = 1 To iterations '' execute ? number of times to complete the puzzle
  25.    For i = 1 To 9
  26.         For j = 1 To 9
  27.            
  28.             sqr = i
  29.             num = j
  30.            
  31.             ''''' in hindsight, this block of code is unnecessary, but it was a proof of concept, so I didn't remove it.
  32.            ''''' "i" gives us the square, and "j" gives us the inner square. We then translate this to row / col.
  33.            If sqr = 1 Or sqr = 4 Or sqr = 7 Then
  34.                 If num = 1 Or num = 4 Or num = 7 Then
  35.                     col = 1
  36.                 ElseIf num = 2 Or num = 5 Or num = 8 Then
  37.                     col = 2
  38.                 ElseIf num = 3 Or num = 6 Or num = 9 Then
  39.                     col = 3
  40.                 End If
  41.             ElseIf sqr = 2 Or sqr = 5 Or sqr = 8 Then
  42.                 If num = 1 Or num = 4 Or num = 7 Then
  43.                     col = 4
  44.                 ElseIf num = 2 Or num = 5 Or num = 8 Then
  45.                     col = 5
  46.                 ElseIf num = 3 Or num = 6 Or num = 9 Then
  47.                     col = 6
  48.                 End If
  49.             ElseIf sqr = 3 Or sqr = 6 Or sqr = 9 Then
  50.                 If num = 1 Or num = 4 Or num = 7 Then
  51.                     col = 7
  52.                 ElseIf num = 2 Or num = 5 Or num = 8 Then
  53.                     col = 8
  54.                 ElseIf num = 3 Or num = 6 Or num = 9 Then
  55.                     col = 9
  56.                 End If
  57.             End If
  58.            
  59.             If sqr = 1 Or sqr = 2 Or sqr = 3 Then
  60.                 If num = 1 Or num = 2 Or num = 3 Then
  61.                     row = 1
  62.                 ElseIf num = 4 Or num = 5 Or num = 6 Then
  63.                     row = 2
  64.                 ElseIf num = 7 Or num = 8 Or num = 9 Then
  65.                     row = 3
  66.                 End If
  67.             ElseIf sqr = 4 Or sqr = 5 Or sqr = 6 Then
  68.                 If num = 1 Or num = 2 Or num = 3 Then
  69.                     row = 4
  70.                 ElseIf num = 4 Or num = 5 Or num = 6 Then
  71.                     row = 5
  72.                 ElseIf num = 7 Or num = 8 Or num = 9 Then
  73.                     row = 6
  74.                 End If
  75.             ElseIf sqr = 7 Or sqr = 8 Or sqr = 9 Then
  76.                 If num = 1 Or num = 2 Or num = 3 Then
  77.                     row = 7
  78.                 ElseIf num = 4 Or num = 5 Or num = 6 Then
  79.                     row = 8
  80.                 ElseIf num = 7 Or num = 8 Or num = 9 Then
  81.                     row = 9
  82.                 End If
  83.             End If
  84.            
  85.             '''''if cell is empty, set the array value to 0. Otherwise, set it to the value.
  86.            If Cells(row, col).Value = "" Then
  87.                 s9(row, col) = 0
  88.             ElseIf Cells(row, col).Value < 10 Then
  89.                 s9(row, col) = Cells(row, col).Value
  90.             End If
  91.            
  92.         Next j
  93.     Next i
  94.    
  95.     ''''' if the cell value is less than 10, store it in the array
  96.    For j = 1 To 9
  97.         For k = 1 To 9
  98.             If Cells(j, k).Value < 10 Then
  99.                 s9(j, k) = Cells(j, k).Value
  100.             Else
  101.                 Cells(j, k).Value = ""
  102.             End If
  103.         Next k
  104.     Next j
  105.    
  106.     ''''' "i" is the number we're checking.
  107.    For i = 1 To 9
  108.         For j = 1 To 9
  109.             For k = 1 To 9
  110.                 If s9(j, k) = i Then ' if array value = the number we're checking
  111.                    rows(j, i) = 1 'mark the the row array for that number / row combination as populated (by using a 1)
  112.                End If
  113.                
  114.                 If s9(k, j) = i Then ' if array value = the number we're checking
  115.                    cols(j, i) = 1 'mark the the column array for that number / column combination as populated (by using a 1)
  116.                End If
  117.             Next k
  118.         Next j
  119.        
  120.         For j = 1 To 9
  121.             For k = 1 To 9
  122.            
  123.                 If rows(j, i) = 0 And cols(k, i) = 0 And s9(j, k) = 0 Then '' if the array value is empty (meaning not a set number), and the row / column doesn't have a set number
  124.                    Cells(j, k).Value = Cells(j, k).Value & i ''mark the cell as a possibility for this number
  125.                End If
  126.            
  127.             Next k
  128.         Next j
  129.     Next i
  130.    
  131.     For i = 1 To 9
  132.         For j = 1 To 9
  133.        
  134.         osCount = 0 ''reset the count to check if the number ("i") only has one occurrence with a square
  135.            For k = 1 To 9
  136.            
  137.                 sqr = j
  138.                 num = k
  139.            
  140.                 If sqr = 1 Or sqr = 4 Or sqr = 7 Then
  141.                     If num = 1 Or num = 4 Or num = 7 Then
  142.                         col = 1
  143.                     ElseIf num = 2 Or num = 5 Or num = 8 Then
  144.                         col = 2
  145.                     ElseIf num = 3 Or num = 6 Or num = 9 Then
  146.                         col = 3
  147.                     End If
  148.                 ElseIf sqr = 2 Or sqr = 5 Or sqr = 8 Then
  149.                     If num = 1 Or num = 4 Or num = 7 Then
  150.                         col = 4
  151.                     ElseIf num = 2 Or num = 5 Or num = 8 Then
  152.                         col = 5
  153.                     ElseIf num = 3 Or num = 6 Or num = 9 Then
  154.                         col = 6
  155.                     End If
  156.                 ElseIf sqr = 3 Or sqr = 6 Or sqr = 9 Then
  157.                     If num = 1 Or num = 4 Or num = 7 Then
  158.                         col = 7
  159.                     ElseIf num = 2 Or num = 5 Or num = 8 Then
  160.                         col = 8
  161.                     ElseIf num = 3 Or num = 6 Or num = 9 Then
  162.                         col = 9
  163.                     End If
  164.                 End If
  165.                
  166.                 If sqr = 1 Or sqr = 2 Or sqr = 3 Then
  167.                     If num = 1 Or num = 2 Or num = 3 Then
  168.                         row = 1
  169.                     ElseIf num = 4 Or num = 5 Or num = 6 Then
  170.                         row = 2
  171.                     ElseIf num = 7 Or num = 8 Or num = 9 Then
  172.                         row = 3
  173.                     End If
  174.                 ElseIf sqr = 4 Or sqr = 5 Or sqr = 6 Then
  175.                     If num = 1 Or num = 2 Or num = 3 Then
  176.                         row = 4
  177.                     ElseIf num = 4 Or num = 5 Or num = 6 Then
  178.                         row = 5
  179.                     ElseIf num = 7 Or num = 8 Or num = 9 Then
  180.                         row = 6
  181.                     End If
  182.                 ElseIf sqr = 7 Or sqr = 8 Or sqr = 9 Then
  183.                     If num = 1 Or num = 2 Or num = 3 Then
  184.                         row = 7
  185.                     ElseIf num = 4 Or num = 5 Or num = 6 Then
  186.                         row = 8
  187.                     ElseIf num = 7 Or num = 8 Or num = 9 Then
  188.                         row = 9
  189.                     End If
  190.                 End If
  191.                
  192.                 If Cells(row, col).Value = i Then
  193.                     square(i, sqr) = 1
  194.                 End If
  195.                
  196.                 If InStr(CStr(Cells(row, col).Value), i) Then ''if the number is found in a square
  197.                    osCount = osCount + 1 ''set "osCount" to the number of occurrences of that number within that square
  198.                End If
  199.                
  200.             Next k
  201.            
  202.             If osCount = 1 Then ''if the number only occured once
  203.                onlySquare(i, j) = 1 ''mark that square "j" as only having one occurrence of number "i"
  204.            End If
  205.         Next j
  206.        
  207.     Next i
  208.    
  209.     For i = 1 To 9
  210.         For j = 1 To 9
  211.             For k = 1 To 9
  212.            
  213.                 sqr = j
  214.                 num = k
  215.            
  216.                 If sqr = 1 Or sqr = 4 Or sqr = 7 Then
  217.                     If num = 1 Or num = 4 Or num = 7 Then
  218.                         col = 1
  219.                     ElseIf num = 2 Or num = 5 Or num = 8 Then
  220.                         col = 2
  221.                     ElseIf num = 3 Or num = 6 Or num = 9 Then
  222.                         col = 3
  223.                     End If
  224.                 ElseIf sqr = 2 Or sqr = 5 Or sqr = 8 Then
  225.                     If num = 1 Or num = 4 Or num = 7 Then
  226.                         col = 4
  227.                     ElseIf num = 2 Or num = 5 Or num = 8 Then
  228.                         col = 5
  229.                     ElseIf num = 3 Or num = 6 Or num = 9 Then
  230.                         col = 6
  231.                     End If
  232.                 ElseIf sqr = 3 Or sqr = 6 Or sqr = 9 Then
  233.                     If num = 1 Or num = 4 Or num = 7 Then
  234.                         col = 7
  235.                     ElseIf num = 2 Or num = 5 Or num = 8 Then
  236.                         col = 8
  237.                     ElseIf num = 3 Or num = 6 Or num = 9 Then
  238.                         col = 9
  239.                     End If
  240.                 End If
  241.                
  242.                 If sqr = 1 Or sqr = 2 Or sqr = 3 Then
  243.                     If num = 1 Or num = 2 Or num = 3 Then
  244.                         row = 1
  245.                     ElseIf num = 4 Or num = 5 Or num = 6 Then
  246.                         row = 2
  247.                     ElseIf num = 7 Or num = 8 Or num = 9 Then
  248.                         row = 3
  249.                     End If
  250.                 ElseIf sqr = 4 Or sqr = 5 Or sqr = 6 Then
  251.                     If num = 1 Or num = 2 Or num = 3 Then
  252.                         row = 4
  253.                     ElseIf num = 4 Or num = 5 Or num = 6 Then
  254.                         row = 5
  255.                     ElseIf num = 7 Or num = 8 Or num = 9 Then
  256.                         row = 6
  257.                     End If
  258.                 ElseIf sqr = 7 Or sqr = 8 Or sqr = 9 Then
  259.                     If num = 1 Or num = 2 Or num = 3 Then
  260.                         row = 7
  261.                     ElseIf num = 4 Or num = 5 Or num = 6 Then
  262.                         row = 8
  263.                     ElseIf num = 7 Or num = 8 Or num = 9 Then
  264.                         row = 9
  265.                     End If
  266.                 End If
  267.                
  268.                 If Cells(row, col).Value <> i And square(i, sqr) = 1 Then '' if a specific row / col isn't already number "i", and there's an existing number "i" within the specified square
  269.                    If InStr(CStr(Cells(row, col).Value), i) <> 0 Then '' if the cell is marked that number "i" is a possibility
  270.                        Cells(row, col).Value = Replace(CStr(Cells(row, col).Value), CStr(i), "") ''remove number "i" as a possibility for that cell
  271.                    End If
  272.                 End If
  273.                
  274.                 If InStr(CStr(Cells(row, col).Value), i) <> 0 And onlySquare(i, sqr) = 1 Then '' if a cell is marked that number "i" is a possibility, and it's the only possibility
  275.                    Cells(row, col).Value = i '' set the cell to that number
  276.                End If
  277.                
  278.             Next k
  279.         Next j
  280.     Next i
  281.    
  282.     For i = 1 To 9
  283.         For j = 1 To 9
  284.            
  285.             orCount = 0 ''reset the count to check if the number ("i") only has one occurrence with a row
  286.            ocCount = 0 ''reset the count to check if the number ("i") only has one occurrence with a column
  287.            For k = 1 To 9
  288.            
  289.                 If InStr(CStr(Cells(j, k).Value), i) <> 0 Then '' if number "i" is found in a row
  290.                    orCount = orCount + 1 ''increase the occurrence count
  291.                End If
  292.                
  293.                 If InStr(CStr(Cells(k, j).Value), i) <> 0 Then '' if number "i" is found in a column
  294.                    ocCount = ocCount + 1 ''increase the occurrence count
  295.                End If
  296.                
  297.             Next k
  298.            
  299.             If orCount = 1 Then '' if the number of occurences = 1
  300.                onlyRow(i, j) = 1 ''set the array value as 1 (bool yes), to mark is as the only occurrence in that row
  301.            End If
  302.            
  303.             If ocCount = 1 Then '' if the number of occurences = 1
  304.                onlyCol(i, j) = 1 ''set the array value as 1 (bool yes), to mark is as the only occurrence in that column
  305.            End If
  306.         Next j
  307.        
  308.         For j = 1 To 9
  309.             For k = 1 To 9
  310.            
  311.                 If InStr(CStr(Cells(j, k).Value), i) <> 0 And onlyRow(i, j) = 1 Then ''if a cell contains number "i", and it's the only occurrence in that row
  312.                    Cells(j, k).Value = i '' set the cell to "i"
  313.                End If
  314.                
  315.                 If InStr(CStr(Cells(j, k).Value), i) <> 0 And onlyCol(i, k) = 1 Then ''if a cell contains number "i", and it's the only occurrence in that columns
  316.                    Cells(j, k).Value = i '' set the cell to "i"
  317.                End If
  318.                
  319.             Next k
  320.         Next j
  321.        
  322.     Next i
  323.    
  324. Next x
  325.  
  326. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement