-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrid.rb
More file actions
34 lines (33 loc) · 783 Bytes
/
grid.rb
File metadata and controls
34 lines (33 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Grid
# Create a rows x columns grid
def initialize(rows, columns)
@rows = rows
@columns = columns
@grid = Array.new(@rows) { Array.new(@columns) }
end
# Generate [row,column] pairs that will cover the grid with a walk that
# doesn't return a pair until all pairs with a lower row or colum value have
# already been returned. For a 3x3 grid, this might return points in the
# order represented in the picture by a..i
#
# abf
# ceg
# dhi
#
# Note that this is also a valid sequence:
#
# abc
# def
# ghi
def cover
# TODO: return the order implied by the first example
@rows.times do |r|
@columns.times do |c|
yield r,c
end
end
end
def [](r)
@grid[r]
end
end