Created
June 5, 2012 13:25
-
-
Save joecannatti/2874961 to your computer and use it in GitHub Desktop.
pe18and67.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
triangle = [] | |
File.open("data.txt").each do |line| | |
to_num = line.split.map(&:to_i) | |
triangle << to_num | |
end | |
def reduce_last_row(triangle) | |
row_index = triangle.size - 2 | |
triangle[row_index].each_with_index do |value,col_index| | |
left = triangle[row_index+1][col_index] | |
right = triangle[row_index+1][col_index+1] | |
triangle[row_index][col_index] = triangle[row_index][col_index] + [left,right].max | |
end | |
triangle.pop | |
end | |
while triangle.size > 1 | |
reduce_last_row(triangle) | |
end | |
p triangle[0][0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment