funmain(args: Array<String>) { val cursor = Cursor(-1, 0) val rect = makeRect(3) rect.print()
clockwise(rect, 0, cursor) }
typealias Matrix = Array<IntArray>
dataclassCursor(var x: Int, var y: Int) { funup() = apply { y -= 1 } fundown() = apply { y += 1 } funleft() = apply { x -= 1 } funright() = apply { x += 1 } }
funclockwise(rect: Matrix, level: Int, cursor: Cursor) { val rectLength = rect[0].size val currentLength = rectLength - 2 * level
for (i in0 until currentLength) { cursor.right() print(rect[cursor]) } for (i in0 until currentLength - 1) { cursor.down() print(rect[cursor]) } for (i in0 until currentLength - 1) { cursor.left() print(rect[cursor]) } for (i in0 until currentLength - 2) { cursor.up() print(rect[cursor]) }