Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Created June 25, 2024 16:31
Show Gist options
  • Save thomasjslone/3ddf594716e156e8f7c15f777dec46eb to your computer and use it in GitHub Desktop.
Save thomasjslone/3ddf594716e156e8f7c15f777dec46eb to your computer and use it in GitHub Desktop.
gpt actually did ok
def draw_spiral(size)
grid = Array.new(size * 2 + 1) { Array.new(size * 2 + 1, ' ') }
x, y = size, size
direction = 0 # 0: right, 1: down, 2: left, 3: up
step = 1
(size * size).times do |i|
grid[y][x] = '*'
case direction
when 0
x += 1
when 1
y += 1
when 2
x -= 1
when 3
y -= 1
end
# Change direction if necessary
case direction
when 0 # moving right
if x > size + step
direction = 1 # change to down
end
when 1 # moving down
if y > size + step
direction = 2 # change to left
step += 1
end
when 2 # moving left
if x < size - step
direction = 3 # change to up
end
when 3 # moving up
if y < size - step
direction = 0 # change to right
step += 1
end
end
end
# Convert grid to a string representation
grid.map { |row| row.join }.join("\n")
end
# Example usage:
size = 20
puts draw_spiral(size)
##
## lets build our own version of this where we dont actually manually draw every square of the spiral but compute the lines from top to bottom
## later cause im lazy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment