Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,124 @@
import Foundation
typealias SodukuPuzzle = [[Int]]
class Soduku {
let mBoardSize:Int!
let mBoxSize:Int!
var mBoard:SodukuPuzzle!
var mRowSubset:[[Bool]]!
var mColSubset:[[Bool]]!
var mBoxSubset:[[Bool]]!
init(board:SodukuPuzzle) {
mBoard = board
mBoardSize = board.count
mBoxSize = Int(sqrt(Double(mBoardSize)))
mRowSubset = [[Bool]](count: mBoardSize, repeatedValue: [Bool](count: mBoardSize, repeatedValue: false))
mColSubset = [[Bool]](count: mBoardSize, repeatedValue: [Bool](count: mBoardSize, repeatedValue: false))
mBoxSubset = [[Bool]](count: mBoardSize, repeatedValue: [Bool](count: mBoardSize, repeatedValue: false))
initSubsets()
}
func computeBoxNo(i:Int, _ j:Int) -> Int {
let boxRow = i / mBoxSize
let boxCol = j / mBoxSize
return boxRow * mBoxSize + boxCol
}
func initSubsets() {
for i in 0..<mBoard.count {
for j in 0..<mBoard.count {
let value = mBoard[i][j]
if value != 0 {
setSubsetValue(i, j, value, true);
}
}
}
}
func isValid(i:Int, _ j:Int, var _ val:Int) -> Bool {
val--
let isPresent = mRowSubset[i][val] || mColSubset[j][val] || mBoxSubset[computeBoxNo(i, j)][val]
return !isPresent
}
func printBoard() {
for i in 0..<mBoardSize {
if i % mBoxSize == 0 {
println(" -----------------------")
}
for j in 0..<mBoardSize {
if j % mBoxSize == 0 {
print("| ")
}
print(mBoard[i][j] != 0 ? String(mBoard[i][j]) : " ")
print(" ")
}
println("|")
}
println(" -----------------------")
}
func setSubsetValue(i:Int, _ j:Int, _ value:Int, _ present:Bool) {
mRowSubset[i][value - 1] = present
mColSubset[j][value - 1] = present
mBoxSubset[computeBoxNo(i, j)][value - 1] = present
}
func solve() {
solve(0, 0)
}
func solve(var i:Int, var _ j:Int) -> Bool {
if i == mBoardSize {
i = 0
j++
if j == mBoardSize {
return true
}
}
if mBoard[i][j] != 0 {
return solve(i + 1, j)
}
for value in 1...mBoardSize {
if isValid(i, j, value) {
mBoard[i][j] = value
setSubsetValue(i, j, value, true)
if solve(i + 1, j) {
return true
}
setSubsetValue(i, j, value, false)
}
}
mBoard[i][j] = 0
return false
}
}
let board = [
[4, 0, 0, 0, 0, 0, 0, 6, 0],
[5, 0, 0, 0, 8, 0, 9, 0, 0],
[3, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 2, 0, 7, 0, 0, 0, 0, 1],
[0, 9, 0, 0, 0, 0, 0, 4, 0],
[8, 0, 0, 0, 0, 3, 0, 5, 0],
[0, 0, 0, 2, 0, 0, 0, 0, 7],
[0, 0, 6, 0, 5, 0, 0, 0, 8],
[0, 1, 0, 0, 0, 0, 0, 0, 6]
]
let puzzle = Soduku(board: board)
puzzle.solve()
puzzle.printBoard()

View file

@ -0,0 +1,64 @@
func solving(board: [[Int]]) -> [[Int]] {
var board = board
var isSolved = false
while !isSolved {
for x in 0 ..< 9 {
for y in 0 ..< 9 {
if board[x][y] == 0 {
let known = Set(board.map { $0[y] } + board[x] + subgrid(board, pos: (x, y)))
let possible = Set(Array(1...9)).subtracting(known)
if possible.count == 1 {
board[x][y] = possible.first!
}
}
}
isSolved = 45 == board[x].reduce(0, +)
}
}
return board
}
func subgrid(_ board: [[Int]], pos: (Int, Int)) -> [Int] {
var r = [Int]()
var (x, y) = pos
x = x / 3 * 3
y = y / 3 * 3
for i in x ..< x + 3 {
for j in y ..< y + 3 {
r.append(board[i][j])
}
}
return r
}
func print(_ board: [[Int]]) {
for i in board.indices {
if i % 9 == 0 {
print(" -------------------")
}
for j in board.indices {
if j % board.count == 0 {
print("| ", terminator: "")
}
let digit = board[i][j]
print(digit != 0 ? digit : " ", terminator: "")
print(" ", terminator: "")
}
print("|")
}
print(" -------------------")
}
let puzzle = [
[0,2,0,4,5,0,7,0,9],
[0,0,0,1,0,9,0,3,0],
[0,0,8,0,0,0,1,0,4],
[0,4,0,0,6,1,0,7,0],
[5,0,6,0,3,0,0,1,0],
[0,3,0,0,0,2,0,9,0],
[3,0,4,0,7,5,0,6,8],
[0,9,0,0,1,0,3,0,7],
[0,0,2,0,0,3,0,0,1]
]
print(solving(board: puzzle))