Represent a square matrix using only List. Access to the matrix must use one-based indices.
interface Cell {
val i: Int
val j: Int
}
interface SquareMatrix {
val width: Int
fun getCell(i: Int, j: Int): Cell
}
class Position(override val i: Int=0, override val j: Int=0) : Cell
val width = 4
val list: List<Cell> by lazy {
mutableListOf<Cell>().apply {
(0 until width*width).forEach {
val column = it % width
val row = (it - column) / width
add(Position(row + 1, column + 1))
}
}.toList()
}
override fun getCell(i: Int, j: Int): Cell {
return list[(j - 1) + ((i - 1) * width)]
}
println(matrix.getCell(1,1))
Position(i=1, j=1)