Skip to content

Instantly share code, notes, and snippets.

@joecannatti
Created June 5, 2012 13:25
Show Gist options
  • Save joecannatti/2874961 to your computer and use it in GitHub Desktop.
Save joecannatti/2874961 to your computer and use it in GitHub Desktop.
pe18and67.rb
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