Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.4 kB
2
Indexable
Never
package main

import (
	"fmt"
	"os"
)

const boardSize = 9

func main() {
	boardStrings := os.Args[1:]
	if len(boardStrings) != boardSize {
		fmt.Println("Error")
		return
	}
	board := make([][]rune, boardSize)
	for i, s := range boardStrings {
		if len(s) != boardSize {
			fmt.Println("Error")
			return
		}
		board[i] = []rune(s)
	}
	if !isValidSudoku(board) || !solveBoard(board) {
		fmt.Println("Error")
		return
	}
	printBoard(board)
}

func printBoard(board [][]rune) {
	for _, row := range board {
		for _, num := range row {
			fmt.Printf("%c ", num)
		}
		fmt.Println()
	}
}

func isValidSudoku(board [][]rune) bool {
	for i := 0; i < boardSize; i++ {
		if !isValidRow(board, i) || !isValidColumn(board, i) || !isValidSubgrid(board, i/3*3, i%3*3) {
			return false
		}
	}
	return true
}

func isValidRow(board [][]rune, row int) bool {
	seen := make(map[rune]bool)
	for _, num := range board[row] {
		if num != '.' && seen[num] {
			return false
		}
		seen[num] = true
	}
	return true
}

func isValidColumn(board [][]rune, col int) bool {
	seen := make(map[rune]bool)
	for i := 0; i < boardSize; i++ {
		num := board[i][col]
		if num != '.' && seen[num] {
			return false
		}
		seen[num] = true
	}
	return true
}

func isValidSubgrid(board [][]rune, startRow, startCol int) bool {
	seen := make(map[rune]bool)
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			num := board[startRow+i][startCol+j]
			if num != '.' && seen[num] {
				return false
			}
			seen[num] = true
		}
	}
	return true
}

func solveBoard(board [][]rune) bool {
	row, col := findEmptyCell(board)
	if row == -1 {
		return true
	}
	for digit := '1'; digit <= '9'; digit++ {
		if isValidPlacement(board, row, col, digit) {
			board[row][col] = digit
			if solveBoard(board) {
				return true
			}
			board[row][col] = '.'
		}
	}
	return false
}

func isValidPlacement(board [][]rune, row, col int, digit rune) bool {
	for i := 0; i < boardSize; i++ {
		if board[row][i] == digit || board[i][col] == digit || board[row/3*3+i/3][col/3*3+i%3] == digit {
			return false
		}
	}
	return true
}

func findEmptyCell(board [][]rune) (int, int) {
	for i, r := range board {
		for j, c := range r {
			if c == '.' {
				return i, j
			}
		}
	}
	return -1, -1
}