Task: Sudoku




Go: package main

import "fmt"


var puzzle = "" +
    "394  267 " +
    "   3  4  " +
    "5  69  2 " +
    " 45   9  " +
    "6       7" +
    "  7   58 " +
    " 1  67  8" +
    "  9  8   " +
    " 264  735"

func main() {
    printGrid("puzzle:", puzzle)
    if s := solve(puzzle); s == "" {
        fmt.Println("no solution")
    } else {
        printGrid("solved:", s)
    }
}


func printGrid(title, s string) {
    fmt.Println(title)
    for r, i := 0, 0; r < 9; r, i = r+1, i+9 {
        fmt.Printf("%c %c %c | %c %c %c | %c %c %c\n", s[i], s[i+1], s[i+2],
            s[i+3], s[i+4], s[i+5], s[i+6], s[i+7], s[i+8])
        if r == 2 || r == 5 {
            fmt.Println("------+-------+------")
        }
    }
}   
    



func solve(u string) string {
    
    
    d := newDlxObject(324)
    
    for r, i := 0, 0; r < 9; r++ {
        for c := 0; c < 9; c, i = c+1, i+1 {
            b := r/3*3 + c/3
            n := int(u[i] - '1')
            if n >= 0 && n < 9 {
                d.addRow([]int{i, 81 + r*9 + n, 162 + c*9 + n,
                    243 + b*9 + n})
            } else {
                for n = 0; n < 9; n++ {
                    d.addRow([]int{i, 81 + r*9 + n, 162 + c*9 + n,
                        243 + b*9 + n})
                }
            }
        }
    }
    
    d.search()
    
    return d.text()
}


type x struct {
    c          *y
    u, d, l, r *x
    
    
    x0 *x
}


type y struct {
    x
    s int 
    n int 
}


type dlx struct {
    ch []y  
    h  *y   
    o  []*x 
}


func newDlxObject(nCols int) *dlx {
    ch := make([]y, nCols+1)
    h := &ch[0]
    d := &dlx{ch, h, nil}
    h.c = h
    h.l = &ch[nCols].x
    ch[nCols].r = &h.x
    nh := ch[1:]
    for i := range ch[1:] {
        hi := &nh[i]
        ix := &hi.x
        hi.n = i
        hi.c = hi
        hi.u = ix
        hi.d = ix
        hi.l = &h.x
        h.r = ix
        h = hi
    }
    return d
}   
    

func (d *dlx) addRow(nr []int) {
    if len(nr) == 0 {
        return
    }
    r := make([]x, len(nr))
    x0 := &r[0]
    for x, j := range nr {
        ch := &d.ch[j+1]
        ch.s++
        np := &r[x]
        np.c = ch
        np.u = ch.u
        np.d = &ch.x
        np.l = &r[(x+len(r)-1)%len(r)]
        np.r = &r[(x+1)%len(r)]
        np.u.d, np.d.u, np.l.r, np.r.l = np, np, np, np
        np.x0 = x0
    }
}


func (d *dlx) text() string {
    b := make([]byte, len(d.o))
    for _, r := range d.o {
        x0 := r.x0
        b[x0.c.n] = byte(x0.r.c.n%9) + '1'
    }
    return string(b)
}   
    

func (d *dlx) search() bool {
    h := d.h
    j := h.r.c
    if j == h {
        return true
    }
    c := j 
    for minS := j.s; ; {
        j = j.r.c
        if j == h {
            break
        }
        if j.s < minS {
            c, minS = j, j.s
        }
    }

    cover(c)
    k := len(d.o)
    d.o = append(d.o, nil)
    for r := c.d; r != &c.x; r = r.d {
        d.o[k] = r
        for j := r.r; j != r; j = j.r {
            cover(j.c)
        }
        if d.search() {
            return true
        }
        r = d.o[k]
        c = r.c
        for j := r.l; j != r; j = j.l {
            uncover(j.c)
        }
    }
    d.o = d.o[:len(d.o)-1]
    uncover(c)
    return false
}

func cover(c *y) {
    c.r.l, c.l.r = c.l, c.r
    for i := c.d; i != &c.x; i = i.d {
        for j := i.r; j != i; j = j.r {
            j.d.u, j.u.d = j.u, j.d
            j.c.s--
        }
    }
}

func uncover(c *y) {
    for i := c.u; i != &c.x; i = i.u {
        for j := i.l; j != i; j = j.l {
            j.c.s++
            j.d.u, j.u.d = j, j
        }
    }
    c.r.l, c.l.r = &c.x, &c.x
}





VB: Dim grid(9, 9)
Dim gridSolved(9, 9)

Public Sub Solve(i, j)
  If i > 9 Then
    
    For r = 1 To 9
      For c = 1 To 9
        gridSolved(r, c) = grid(r, c)
      Next c
    Next r
    Exit Sub
  End If
  For n = 1 To 9
    If isSafe(i, j, n) Then
      nTmp = grid(i, j)
      grid(i, j) = n
      If j = 9 Then
        Solve i + 1, 1
      Else
        Solve i, j + 1
      End If
      grid(i, j) = nTmp
    End If
  Next n
End Sub

Public Function isSafe(i, j, n) As Boolean
Dim iMin As Integer
Dim jMin As Integer

If grid(i, j) <> 0 Then
  isSafe = (grid(i, j) = n)
  Exit Function
End If



For c = 1 To 9
  If grid(i, c) = n Then
    isSafe = False
    Exit Function
  End If
Next c


For r = 1 To 9
 If grid(r, j) = n Then
   isSafe = False
   Exit Function
 End If
Next r


iMin = 1 + 3 * Int((i - 1) / 3)
jMin = 1 + 3 * Int((j - 1) / 3)
For r = iMin To iMin + 2
  For c = jMin To jMin + 2
    If grid(r, c) = n Then
      isSafe = False
      Exit Function
    End If
  Next c
Next r


isSafe = True
End Function

Public Sub Sudoku()
  
  
  

  Dim s(9) As String

  
  s(1) = "001005070"
  s(2) = "920600000"
  s(3) = "008000600"
  s(4) = "090020401"
  s(5) = "000000000"
  s(6) = "304080090"
  s(7) = "007000300"
  s(8) = "000007069"
  s(9) = "010800700"
  For i = 1 To 9
    For j = 1 To 9
      grid(i, j) = Int(Val(Mid$(s(i), j, 1)))
    Next j
  Next i
  
  Solve 1, 1
  
  Debug.Print "Solution:"
  For i = 1 To 9
    For j = 1 To 9
      Debug.Print Format$(gridSolved(i, j)); " ";
    Next j
    Debug.Print
  Next i
End Sub