Skip to content

Instantly share code, notes, and snippets.

@stephaneliu
Created August 15, 2021 19:18
Show Gist options
  • Save stephaneliu/58d35c3f669d577baf77b74388832056 to your computer and use it in GitHub Desktop.
Save stephaneliu/58d35c3f669d577baf77b74388832056 to your computer and use it in GitHub Desktop.
# https://replit.com/join/eugzahzgxx-stephaneliu1
module GridPrinter
def print
sleep(0.2)
system 'clear'
(1..grid_size).each do |row|
puts grid[row][1..grid_size].join(' ')
end
end
end
class Point
attr_reader :x, :y
def initialize(x:, y:, place_holder: '**')
@x = x
@y = y
@place_holder = place_holder
end
end
class GridBuilder
attr_reader :grid, :grid_size, :place_holder
def initialize(grid_size:, place_holder: '**')
@grid = []
@grid_size = grid_size
@place_holder = place_holder
end
def build
(1..grid_size).each do |row|
grid[row] = Array.new(grid_size + 1) do |col|
Point.new(x: row, y: col)
place_holder
end
end
end
end
class Dot
include GridPrinter
attr_reader :steps, :grid_size, :grid_builder, :grid
def initialize(steps: 25, grid_size: 9, grid_builder: GridBuilder)
@steps = steps
@grid_size = grid_size
@grid_builder = grid_builder.new(grid_size: grid_size)
end
def self.spiral
self.new.spiral
end
def spiral
start
@facing_direction = take_first_step
(3..steps).each do |step|
# sleep(0.2)
@current_step = step
walk
# system 'clear'
print
end
"Done!"
end
private
attr_reader :current_step, :facing_direction, :currently_at
def start
return true if started?
grid_builder.build
@grid = grid_builder.grid
@grid[5][5] = print_step(1)
true
end
def walk
step = next_step[facing_direction]
# point = point.advance(grid, current_step)
@facing_direction = if turnable_at?(step)
turn(step)
else
forward(step)
end
end
def take_first_step
@grid[6][5] = print_step(first_step)
@currently_at = [6, 5]
:south
end
def turn(step)
@grid[currently_at.first + step[:turn].first][currently_at[1] + step[:turn][1]] = print_step(current_step)
@currently_at = [currently_at.first + step[:turn].first, currently_at[1] + step[:turn][1]]
step[:turn][2]
end
def forward(step)
@grid[currently_at.first + step[:forward].first][currently_at[1] + step[:forward][1]] = print_step(current_step)
@currently_at = [currently_at.first + step[:forward].first, currently_at[1] + step[:forward][1]]
step[:forward][2]
end
def turnable_at?(step)
grid[currently_at.first + step[:turn].first][currently_at[1] + step[:turn][1]] == '**'
end
def started?
return false unless grid
grid[5][5] == print_step(1)
end
def first_step?
current_step == first_step
end
def first_step
2
end
def next_step
{
north: { turn: [0, 1, :east], forward: [-1, 0, :north] },
east: { turn: [1, 0, :south], forward: [0, 1, :east] },
south: { turn: [0, -1, :west], forward: [1, 0, :south] },
west: { turn: [-1, 0, :north], forward: [0, -1, :west] }
}
end
def print_step(step)
case step
when 1..9
"0#{step}"
when 1..99
step
else
step
end
end
end
dot = Dot.new
dot.spiral
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment