-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path015_lattice_paths.rb
38 lines (31 loc) · 1.06 KB
/
015_lattice_paths.rb
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
35
36
37
38
# Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down,
# there are exactly 6 routes to the bottom right corner.
# How many such routes are there through a 20×20 grid?
require 'benchmark'
class Grid
#Ultimately, the question reduces to this: You must go down x number of times
#you must go right y number of times (x and y may equal each other).
#how many permutations of the path exist when selecting steps?
#the answer is (x + y)! / (x! * y!), since any given x is intertchangable with
#any other.
def initialize(right, down)
#I'm using two seperate variables for the two dimensions of the grid
#so that this program will work on non-square grids
@right = right
@down = down
@paths = routes
end
def routes
routes = factorial(@right + @down) / (factorial(@right) * factorial(@down))
end
def factorial(number)
if number == 0
factorial = 1
else
factorial = number * factorial(number - 1)
end
factorial
end
end
puts Benchmark.measure { new_grid = Grid.new(5, 5).paths }
puts Grid.new(5, 5).paths