Task

Produce a zig-zag array.



A   zig-zag   array is a square arrangement of the first   N2   natural numbers,   where the

numbers increase sequentially as you zig-zag along the array's   anti-diagonals.

For a graphical representation, see   JPG zigzag   (JPG uses such arrays to encode images).



For example, given   5,   produce this array:

 0  1  5  6 14
 2  4  7 13 15
 3  8 12 16 21
 9 11 17 20 22
10 18 19 23 24



Related tasks

   Spiral matrix
   Identity matrix
   Ulam spiral (for primes)


See also

   Wiktionary entry:   anti-diagonals


// version 1.1.3
 
typealias Vector = IntArray
typealias Matrix = Array<Vector>
 
fun zigzagMatrix(n: Int): Matrix {
    val result = Matrix(n) { Vector(n) }
    var down = false
    var count = 0
    for (col in 0 until n) {
        if (down) 
            for (row in 0..col) result[row][col - row] = count++
        else 
            for (row in col downTo 0) result[row][col - row] = count++
        down = !down
    }
    for (row in 1 until n) {
        if (down)
           for (col in n - 1 downTo row) result[row + n - 1 - col][col] = count++
        else
           for (col in row until n) result[row + n - 1 - col][col] = count++
        down = !down
    }
    return result
}
fun printMatrix(m: Matrix) {
    for (i in 0 until m.size) {
        for (j in 0 until m.size) print("%2d ".format(m[i][j]))
        println()
    }
    println()
}
 
fun main(args: Array<String>) {
    printMatrix(zigzagMatrix(5))
    printMatrix(zigzagMatrix(10))
}