Skip to content

Instantly share code, notes, and snippets.

@rossta
Last active April 9, 2016 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossta/f1576f6e944cdcfd0876907ccd4ae314 to your computer and use it in GitHub Desktop.
Save rossta/f1576f6e944cdcfd0876907ccd4ae314 to your computer and use it in GitHub Desktop.
Solution to Ancient City Ruby 2016
# I gave a take about Enumerator at ACR so naturally
# I had to solve the Snake Case challenge using one.
module SnakeCase
module_function
# Returns the number of paths for a
# grid given by m x n blocks
#
# @param m [Fixnum] width
# @param n [Fixnum] length
# @return [Fixnum] number of paths
def paths(m, n)
pascal.take(m+n)[-1][n]
end
# Returns a sequence of arrays representing
# pascals triangle where the values in each
# "row" represent the sum of adjacent row values
# in the preceding row
#
# @return [Array] an array of arrays
def pascal(rows = [1])
Enumerator.new do |y|
loop { y.yield(rows = ([0] + rows).zip(rows + [0]).map { |a, b| a + b }) }
end
end
end
SnakeCase.paths(10, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment