Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infrastructure Technical Challenge #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion sudoku.go
Original file line number Diff line number Diff line change
@@ -1 +1,100 @@
package main
// sudoku.go
package main

import "fmt"

const gridSize = 9

// isValid checks whether placing num at grid[row][col] is a valid move
func isValid(grid [][]int, row, col, num int) bool {
// Check if num exists in the same row or column
for i := 0; i < gridSize; i++ {
if grid[row][i] == num || grid[i][col] == num {
return false
}
}

// Check if num exists in the 3x3 subgrid
startRow, startCol := 3*(row/3), 3*(col/3)
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
if grid[startRow+i][startCol+j] == num {
return false
}
}
}

return true
}

// SolveSudoku uses backtracking to solve the Sudoku puzzle
func SolveSudoku(grid [][]int) [][]int {
if solveSudoku(grid) {
return grid
}
return nil
}


// solveSudoku recursively tries to solve the Sudoku puzzle
func solveSudoku(grid [][]int) bool {
// Loop through each cell in the Sudoku grid
for row := 0; row < gridSize; row++ {
for col := 0; col < gridSize; col++ {
// Check if the current cell is empty (contains 0)
if grid[row][col] == 0 {
// Try placing numbers 1 to 9 in the current cell
for num := 1; num <= 9; num++ {
// Check if placing num in the current cell is valid
if isValid(grid, row, col, num) {
// If valid, set the cell to num and recursively try to solve the puzzle
grid[row][col] = num
if solveSudoku(grid) {
return true // Return true if a solution is found
}
// If the recursive call did not find a solution, backtrack by setting the cell back to 0
grid[row][col] = 0
}
}
// If no valid number is found for the current cell, backtrack
return false
}
}
}
// If all cells are filled, print the solution
printGrid(grid)
return true
}

// printGrid prints the Sudoku grid from the given input grid
func printGrid(grid [][]int) {
for _, row := range grid {
fmt.Println(row)
}
}


func main() {
// Example input
input := [][]int{
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9},
}

// Solve the Sudoku puzzle
solved := SolveSudoku(input)

if solved != nil {
fmt.Println("Solved Sudoku:")
printGrid(solved)
} else {
fmt.Println("No solution exists.")
}
}
74 changes: 37 additions & 37 deletions sudoku_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package main

import (
"reflect"
"testing"
)

func TestSolveSudoku(t *testing.T) {
input := [][]int{
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9},
}

expected := [][]int{
{5, 3, 4, 6, 7, 8, 9, 1, 2},
{6, 7, 2, 1, 9, 5, 3, 4, 8},
{1, 9, 8, 3, 4, 2, 5, 6, 7},
{8, 5, 9, 7, 6, 1, 4, 2, 3},
{4, 2, 6, 8, 5, 3, 7, 9, 1},
{7, 1, 3, 9, 2, 4, 8, 5, 6},
{9, 6, 1, 5, 3, 7, 2, 8, 4},
{2, 8, 7, 4, 1, 9, 6, 3, 5},
{3, 4, 5, 2, 8, 6, 1, 7, 9},
}

solved := SolveSudoku(input)

if !reflect.DeepEqual(solved, expected) {
t.Errorf("Sudoku puzzle was not solved correctly. Expected:\n%v\n\nGot:\n%v", expected, solved)
}
package main
import (
"reflect"
"testing"
)
func TestSolveSudoku(t *testing.T) {
input := [][]int{
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9},
}
expected := [][]int{
{5, 3, 4, 6, 7, 8, 9, 1, 2},
{6, 7, 2, 1, 9, 5, 3, 4, 8},
{1, 9, 8, 3, 4, 2, 5, 6, 7},
{8, 5, 9, 7, 6, 1, 4, 2, 3},
{4, 2, 6, 8, 5, 3, 7, 9, 1},
{7, 1, 3, 9, 2, 4, 8, 5, 6},
{9, 6, 1, 5, 3, 7, 2, 8, 4},
{2, 8, 7, 4, 1, 9, 6, 3, 5},
{3, 4, 5, 2, 8, 6, 1, 7, 9},
}
solved := SolveSudoku(input)
if !reflect.DeepEqual(solved, expected) {
t.Errorf("Sudoku puzzle was not solved correctly. Expected:\n%v\n\nGot:\n%v", expected, solved)
}
}