Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub sud()
- Dim s9(9, 9) As Integer 'x/y row/col address
- Dim row As Integer
- Dim col As Integer
- Dim rows(9, 9) As Integer '(x,y) to check if y row contain number x
- Dim cols(9, 9) As Integer '(x,y) to check if y column contains number x
- Dim square(9, 9) As Integer '(x,y) to check if y square contains by number x
- Dim onlySquare(9, 9) As Integer '(x,y) used to check if there's only one occurrence of number x in square y
- Dim onlyRow(9, 9) As Integer '(x,y) used to check if there's only one occurrence of number x in row y
- Dim onlyCol(9, 9) As Integer '(x,y) used to check if there's only one occurrence of number x in column y
- Dim sqr As Integer
- Dim num As Integer
- Dim osCount As Integer 'Only Square Count, temp value to count the number of occurrences of a number within a square
- Dim orCount As Integer 'Only Row Count, temp value to count the number of occurrences of a number within a row
- Dim ocCount As Integer 'Only Column Count, temp value to count the number of occurrences of a number within a column
- Dim iterations As Integer
- iterations = 50 '' the code is intended to iterate on itself for process of elimination
- For x = 1 To iterations '' execute ? number of times to complete the puzzle
- For i = 1 To 9
- For j = 1 To 9
- sqr = i
- num = j
- ''''' in hindsight, this block of code is unnecessary, but it was a proof of concept, so I didn't remove it.
- ''''' "i" gives us the square, and "j" gives us the inner square. We then translate this to row / col.
- If sqr = 1 Or sqr = 4 Or sqr = 7 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 1
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 2
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 3
- End If
- ElseIf sqr = 2 Or sqr = 5 Or sqr = 8 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 4
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 5
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 6
- End If
- ElseIf sqr = 3 Or sqr = 6 Or sqr = 9 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 7
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 8
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 9
- End If
- End If
- If sqr = 1 Or sqr = 2 Or sqr = 3 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 1
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 2
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 3
- End If
- ElseIf sqr = 4 Or sqr = 5 Or sqr = 6 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 4
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 5
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 6
- End If
- ElseIf sqr = 7 Or sqr = 8 Or sqr = 9 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 7
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 8
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 9
- End If
- End If
- '''''if cell is empty, set the array value to 0. Otherwise, set it to the value.
- If Cells(row, col).Value = "" Then
- s9(row, col) = 0
- ElseIf Cells(row, col).Value < 10 Then
- s9(row, col) = Cells(row, col).Value
- End If
- Next j
- Next i
- ''''' if the cell value is less than 10, store it in the array
- For j = 1 To 9
- For k = 1 To 9
- If Cells(j, k).Value < 10 Then
- s9(j, k) = Cells(j, k).Value
- Else
- Cells(j, k).Value = ""
- End If
- Next k
- Next j
- ''''' "i" is the number we're checking.
- For i = 1 To 9
- For j = 1 To 9
- For k = 1 To 9
- If s9(j, k) = i Then ' if array value = the number we're checking
- rows(j, i) = 1 'mark the the row array for that number / row combination as populated (by using a 1)
- End If
- If s9(k, j) = i Then ' if array value = the number we're checking
- cols(j, i) = 1 'mark the the column array for that number / column combination as populated (by using a 1)
- End If
- Next k
- Next j
- For j = 1 To 9
- For k = 1 To 9
- 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
- Cells(j, k).Value = Cells(j, k).Value & i ''mark the cell as a possibility for this number
- End If
- Next k
- Next j
- Next i
- For i = 1 To 9
- For j = 1 To 9
- osCount = 0 ''reset the count to check if the number ("i") only has one occurrence with a square
- For k = 1 To 9
- sqr = j
- num = k
- If sqr = 1 Or sqr = 4 Or sqr = 7 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 1
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 2
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 3
- End If
- ElseIf sqr = 2 Or sqr = 5 Or sqr = 8 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 4
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 5
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 6
- End If
- ElseIf sqr = 3 Or sqr = 6 Or sqr = 9 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 7
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 8
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 9
- End If
- End If
- If sqr = 1 Or sqr = 2 Or sqr = 3 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 1
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 2
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 3
- End If
- ElseIf sqr = 4 Or sqr = 5 Or sqr = 6 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 4
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 5
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 6
- End If
- ElseIf sqr = 7 Or sqr = 8 Or sqr = 9 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 7
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 8
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 9
- End If
- End If
- If Cells(row, col).Value = i Then
- square(i, sqr) = 1
- End If
- If InStr(CStr(Cells(row, col).Value), i) Then ''if the number is found in a square
- osCount = osCount + 1 ''set "osCount" to the number of occurrences of that number within that square
- End If
- Next k
- If osCount = 1 Then ''if the number only occured once
- onlySquare(i, j) = 1 ''mark that square "j" as only having one occurrence of number "i"
- End If
- Next j
- Next i
- For i = 1 To 9
- For j = 1 To 9
- For k = 1 To 9
- sqr = j
- num = k
- If sqr = 1 Or sqr = 4 Or sqr = 7 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 1
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 2
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 3
- End If
- ElseIf sqr = 2 Or sqr = 5 Or sqr = 8 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 4
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 5
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 6
- End If
- ElseIf sqr = 3 Or sqr = 6 Or sqr = 9 Then
- If num = 1 Or num = 4 Or num = 7 Then
- col = 7
- ElseIf num = 2 Or num = 5 Or num = 8 Then
- col = 8
- ElseIf num = 3 Or num = 6 Or num = 9 Then
- col = 9
- End If
- End If
- If sqr = 1 Or sqr = 2 Or sqr = 3 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 1
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 2
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 3
- End If
- ElseIf sqr = 4 Or sqr = 5 Or sqr = 6 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 4
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 5
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 6
- End If
- ElseIf sqr = 7 Or sqr = 8 Or sqr = 9 Then
- If num = 1 Or num = 2 Or num = 3 Then
- row = 7
- ElseIf num = 4 Or num = 5 Or num = 6 Then
- row = 8
- ElseIf num = 7 Or num = 8 Or num = 9 Then
- row = 9
- End If
- End If
- 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
- If InStr(CStr(Cells(row, col).Value), i) <> 0 Then '' if the cell is marked that number "i" is a possibility
- Cells(row, col).Value = Replace(CStr(Cells(row, col).Value), CStr(i), "") ''remove number "i" as a possibility for that cell
- End If
- End If
- 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
- Cells(row, col).Value = i '' set the cell to that number
- End If
- Next k
- Next j
- Next i
- For i = 1 To 9
- For j = 1 To 9
- orCount = 0 ''reset the count to check if the number ("i") only has one occurrence with a row
- ocCount = 0 ''reset the count to check if the number ("i") only has one occurrence with a column
- For k = 1 To 9
- If InStr(CStr(Cells(j, k).Value), i) <> 0 Then '' if number "i" is found in a row
- orCount = orCount + 1 ''increase the occurrence count
- End If
- If InStr(CStr(Cells(k, j).Value), i) <> 0 Then '' if number "i" is found in a column
- ocCount = ocCount + 1 ''increase the occurrence count
- End If
- Next k
- If orCount = 1 Then '' if the number of occurences = 1
- onlyRow(i, j) = 1 ''set the array value as 1 (bool yes), to mark is as the only occurrence in that row
- End If
- If ocCount = 1 Then '' if the number of occurences = 1
- onlyCol(i, j) = 1 ''set the array value as 1 (bool yes), to mark is as the only occurrence in that column
- End If
- Next j
- For j = 1 To 9
- For k = 1 To 9
- 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
- Cells(j, k).Value = i '' set the cell to "i"
- End If
- 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
- Cells(j, k).Value = i '' set the cell to "i"
- End If
- Next k
- Next j
- Next i
- Next x
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement