first commit
This commit is contained in:
commit
59ed4ac5f9
6 changed files with 143 additions and 0 deletions
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
9
.idea/SudokuSolver.iml
generated
Normal file
9
.idea/SudokuSolver.iml
generated
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/SudokuSolver.iml" filepath="$PROJECT_DIR$/.idea/SudokuSolver.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
74
SudokuValidator/Validator.go
Normal file
74
SudokuValidator/Validator.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package SudokuValidator
|
||||
|
||||
type SudokuValidator struct {
|
||||
rows [9]map[byte]bool
|
||||
cols [9]map[byte]bool
|
||||
boxes [9]map[byte]bool
|
||||
}
|
||||
|
||||
func NewSudokuValidator() *SudokuValidator {
|
||||
validator := &SudokuValidator{}
|
||||
for i := 0; i < 9; i++ {
|
||||
validator.rows[i] = make(map[byte]bool)
|
||||
validator.cols[i] = make(map[byte]bool)
|
||||
validator.boxes[i] = make(map[byte]bool)
|
||||
}
|
||||
return validator
|
||||
}
|
||||
|
||||
func (s *SudokuValidator) isValidRow(row int, num byte) bool {
|
||||
if s.rows[row][num] {
|
||||
return false
|
||||
}
|
||||
s.rows[row][num] = true
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SudokuValidator) isValidColumn(col int, num byte) bool {
|
||||
if s.cols[col][num] {
|
||||
return false
|
||||
}
|
||||
s.cols[col][num] = true
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SudokuValidator) isValidBox(boxIndex int, num byte) bool {
|
||||
if s.boxes[boxIndex][num] {
|
||||
return false
|
||||
}
|
||||
s.boxes[boxIndex][num] = true
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SudokuValidator) IsValidSudoku(board *[][]byte) bool {
|
||||
for i := 0; i < 9; i++ {
|
||||
for j := 0; j < 9; j++ {
|
||||
if (*board)[i][j] != '.' {
|
||||
num := (*board)[i][j]
|
||||
boxIndex := (i/3)*3 + j/3
|
||||
|
||||
if !(s.isValidRow(i, num) && s.isValidColumn(j, num) && s.isValidBox(boxIndex, num)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsValidSudoku2 just for demo purposes
|
||||
func (s *SudokuValidator) IsValidSudoku2(board [][]byte) bool {
|
||||
for i := 0; i < 9; i++ {
|
||||
for j := 0; j < 9; j++ {
|
||||
if board[i][j] != '.' {
|
||||
num := board[i][j]
|
||||
boxIndex := (i/3)*3 + j/3
|
||||
|
||||
if !(s.isValidRow(i, num) && s.isValidColumn(j, num) && s.isValidBox(boxIndex, num)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module SudokuSolver
|
||||
|
||||
go 1.22
|
41
main.go
Normal file
41
main.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"SudokuSolver/SudokuValidator"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
board1 := [][]byte{
|
||||
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
|
||||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
|
||||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
|
||||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
|
||||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
|
||||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
|
||||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
|
||||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
|
||||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
|
||||
}
|
||||
|
||||
board2 := [][]byte{
|
||||
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
|
||||
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
|
||||
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
|
||||
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
|
||||
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
|
||||
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
|
||||
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
|
||||
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
|
||||
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
|
||||
}
|
||||
tick := time.Tick(time.Millisecond)
|
||||
start := <-tick
|
||||
validator := SudokuValidator.NewSudokuValidator()
|
||||
fmt.Println("Example 1 is valid:", validator.IsValidSudoku(&board1))
|
||||
fmt.Println("Example 2 is valid:", validator.IsValidSudoku(&board2))
|
||||
end := <-tick
|
||||
elapsed := end.Sub(start)
|
||||
fmt.Println("Time elapsed:", elapsed.Microseconds(), "microseconds")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue