Number Place and Swift 3 (3)

backtrack

The graph shows the value of the index when the recursive function dropNum(index: Int) is called. Note that initially there are 52 empty places to be filled.

    func dropNum(index: Int) {
        var place = emptyPlaces[index]
        for num in 1...9 {
            if isSafeForNum(place: place, num: num) {
                table[place.row][place.col] = num
                putCount += 1
                if putCount == nplaces { return }
                dropNum(index: index+1)
                if putCount != nplaces {
                    place = emptyPlaces[index]
                    table[place.row][place.col] = 0
                    putCount -= 1
                }
            }
        }
    }

You can try the program at the site, IBM Swift Sandbox, by doing copy and paste the following 95 lines.

backtrack2

struct Place {
    var row: Int = -1
    var col: Int = -1
}

class NumBoard {
    var table       = [[Int]]()
    var emptyPlaces = [Place]()
    var place       = Place()
    let nplaces : Int
    var putCount: Int

    init() {
        table = [[7,0,3, 0,0,0, 0,0,0],
                 [0,5,1, 3,7,0, 4,0,0],
                 [6,0,0, 0,1,0, 0,2,0],
                 [0,0,4, 6,0,0, 0,0,0],
                 [0,0,0, 0,0,7, 0,8,0],
                 [5,0,0, 2,3,1, 9,0,0],
                 [3,0,0, 0,2,4, 0,9,1],
                 [0,0,0, 0,0,0, 8,4,0],
                 [0,0,7, 0,9,0, 2,0,0]]
        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    place.row = rr
                    place.col = cc
                    emptyPlaces.append(place)
                 }
            }
        }
        putCount = 0
        nplaces  = emptyPlaces.count
        myprint(table: table)
        print("nplaces = \(nplaces)")
    }

    func myprint(table: [[Int]]) {
        print("-----------------")
        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    print(".", terminator: " ")
                } else {
                    print(table[rr][cc], terminator: " ")
                }
            }
            print("")
        }
        print("-----------------")
    }
    
    func isSafeForNum(place: Place, num: Int) -> Bool {
        for rr in 0...8 {
            if num == table[rr][place.col] { return false }
        }
        for cc in 0...8 {
            if num == table[place.row][cc] { return false }
        }
        let rowBase = (place.row / 3) * 3
        let colBase = (place.col / 3) * 3
        for rr in 0...2 {
            for cc in 0...2 {
                if num == table[rowBase+rr][colBase+cc] { return false }
            }
        }
        return true
    }

    func dropNum(index: Int) {
        var place = emptyPlaces[index]
        for num in 1...9 {
            if isSafeForNum(place: place, num: num) {
                table[place.row][place.col] = num
                putCount += 1
                if putCount == nplaces { return }
                dropNum(index: index+1)
                if putCount != nplaces {
                    place = emptyPlaces[index]
                    table[place.row][place.col] = 0
                    putCount -= 1
                }
            }
        }
    }

    func play() {
        dropNum(index: 0)
        myprint(table: table)
    }
}

// main program
let myboard = NumBoard()
myboard.play()

Number Place and Swift 3 (2)

numberplace2

The code should always be simple, but still not in that stage.

struct Place {
    var row: Int = -1
    var col: Int = -1
}

class NumBoard {
    var table       = [[Int]]()
    var emptyPlaces = [Place]()
    var place       = Place()
    let nplaces : Int
    var putCount: Int

    init() {
        table = [[7,0,3, 0,0,0, 0,0,0],
                 [0,5,1, 3,7,0, 4,0,0],
                 [6,0,0, 0,1,0, 0,2,0],
                 [0,0,4, 6,0,0, 0,0,0],
                 [0,0,0, 0,0,7, 0,8,0],
                 [5,0,0, 2,3,1, 9,0,0],
                 [3,0,0, 0,2,4, 0,9,1],
                 [0,0,0, 0,0,0, 8,4,0],
                 [0,0,7, 0,9,0, 2,0,0]]

        for rr in 0...8 {
            for cc in 0...8 {
                if table[rr][cc] == 0 {
                    place.row = rr
                    place.col = cc
                    emptyPlaces.append(place)
                 }
            }
        }
        putCount = 0
        nplaces = emptyPlaces.count
        myprint(table: table)
    }

    func myprint(table: [[Int]]) {
        print("-----------------")
        for rr in 0...8 {
            for cc in 0...8 {
                print(table[rr][cc], terminator: " ")
            }
            print("")
        }
        print("-----------------")
    }
    
    func isSafeForNum(place: Place, num: Int) -> Bool {
        for rr in 0...8 {
            if num == table[rr][place.col] { return false }
        }
        for cc in 0...8 {
            if num == table[place.row][cc] { return false }
        }
        let rowBase = (place.row / 3) * 3
        let colBase = (place.col / 3) * 3
        for rr in 0...2 {
            for cc in 0...2 {
                if num == table[rowBase+rr][colBase+cc] { return false }
            }
        }
        return true
    }

    func dropNum(index: Int) {
        /* could be better */
    }

    func play() {
        dropNum(index: 0)
        myprint(table: table)
    }
}

let myboard = NumBoard()
myboard.play()

By the way, you may not like the lines such as:

let rowBase = (place.row / 3) * 3
let colBase = (place.col / 3) * 3
  for rr in 0...2 {
    for cc in 0...2 {
      if num == table[rowBase+rr][colBase+cc] { return false }
    }
  }

In my opinion, this is much easier on the eyes than replacing 3 with blockSize and 2 with blockSize-1.

Sunrise, Sunset

I was trying to write a swift 3 code for solving Number Place problems, then the sound came and took away my concentration.

Sunrise, sunset
Sunrise, sunset
Swiftly fly the years
One season following another
Laden with happiness and tears

swift3

The program is not complete yet.

Gmsh

gmsh

Gmsh is a three-dimensional finite element mesh generator.

ElmerGrid is used to translater Gmsh format into ElmerSolver mesh format.

% ElmerGrid 14 2 mesh.msh -autoclean

elmer4

A part of a solver input file is:

Equation 1
  Name = "Equation 1"
  Active Solvers(1) = 1
End

Material 1
  Name = "Aluminium (generic)"
  Heat expansion Coefficient = 23.1e-6
  Heat Conductivity = 237.0
  Sound speed = 5000.0
  Heat Capacity = 897.0
  Mesh Poisson ratio = 0.35
  Density = 2700.0
  Poisson ratio = 0.35
  Youngs modulus = 70.0e9
End

Boundary Condition 1
  Target Boundaries(1) = 1 
  Name = "BoundaryCondition 1"
  Temperature = 100.0
End

Boundary Condition 2
  Target Boundaries(1) = 2 
  Name = "BoundaryCondition 2"
  Temperature = 200.0
End

Boundary Condition 3
  Target Boundaries(1) = 3 
  Name = "BoundaryCondition 3"
  Temperature = 300.0
End

Elmer

elmer

Elmer is an open source multiphysical simulation software.

The figure shows an elastic beam being rigidly supported on one end.