Skip to content

Instantly share code, notes, and snippets.

@rampantmonkey
Last active December 20, 2015 12:48
Show Gist options
  • Save rampantmonkey/6133414 to your computer and use it in GitHub Desktop.
Save rampantmonkey/6133414 to your computer and use it in GitHub Desktop.
Project Euler Problem #11
#!/usr/bin/env ruby
input = ARGV[0]
class Grid
attr_reader :data
def initialize file, count=4
@data = IO.read(file).lines.map{|l| l.split " "}.map{|l| l.map{|i| i.to_i}}
@count = count
end
def maximum_product g
g.map do |line|
line.each_cons(@count).map do |arr|
arr.inject(1, :*)
end
end.flatten.max
end
def horizontal
maximum_product data
end
def vertical
maximum_product data.transpose
end
def right_diagonal
maximum_product data.each_with_index.map{|e,i| e.rotate(i)}.transpose
end
def left_diagonal
maximum_product data.each_with_index.map{|e, i| e.rotate(-i)}.transpose
end
def result
[ vertical, horizontal, left_diagonal, right_diagonal ].max
end
end
puts Grid.new(input).result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment