Version: 0.0.1
Last Updated: Unknown
class SpiralMatrix def self.gen(dim) mat = Array.new(dim) { Array.new(dim)} count = 1 startRow = 0 startCol = 0 endRow = dim - 1 endCol = dim - 1 while startRow <= endRow && startCol <= endCol # top row i = startCol while i <= endCol do mat[startRow][i] = count count += 1 i += 1 end startRow += 1 # far col i = startRow while i <= endRow do mat[i][endCol] = count count += 1 i += 1 end endCol -= 1 # bottow row i = endCol while i >= startCol do mat[endRow][i] = count count += 1 i -= 1 end endRow -= 1 # closest col i = endRow while i >= startRow do mat[i][startCol] = count count += 1 i -= 1 end startCol += 1 end return mat end end