From 59ed4ac5f9dacd40ade20499551bda3016dafb31 Mon Sep 17 00:00:00 2001 From: Zenny <78681533+ZennDev1337@users.noreply.github.com> Date: Thu, 28 Mar 2024 17:29:55 +0100 Subject: [PATCH] first commit --- .idea/.gitignore | 8 ++++ .idea/SudokuSolver.iml | 9 +++++ .idea/modules.xml | 8 ++++ SudokuValidator/Validator.go | 74 ++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ main.go | 41 ++++++++++++++++++++ 6 files changed, 143 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/SudokuSolver.iml create mode 100644 .idea/modules.xml create mode 100644 SudokuValidator/Validator.go create mode 100644 go.mod create mode 100644 main.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/SudokuSolver.iml b/.idea/SudokuSolver.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/SudokuSolver.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a27b896 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/SudokuValidator/Validator.go b/SudokuValidator/Validator.go new file mode 100644 index 0000000..3b6a3dc --- /dev/null +++ b/SudokuValidator/Validator.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e463e8f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module SudokuSolver + +go 1.22 diff --git a/main.go b/main.go new file mode 100644 index 0000000..e957c08 --- /dev/null +++ b/main.go @@ -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") +}