Skip to content

Instantly share code, notes, and snippets.

@joecannatti
Created June 5, 2012 12:55
Show Gist options
  • Save joecannatti/2874788 to your computer and use it in GitHub Desktop.
Save joecannatti/2874788 to your computer and use it in GitHub Desktop.
pe18.rb
triangle = []
File.open("data.txt").each do |line|
triangle << line.split
end
def calc_values(row, value, triangle, results, current_path=0)
if triangle.size == (row + 1)
results << current_path + triangle[row][value].to_i
else
calc_values(row + 1, value, triangle, results, current_path + triangle[row][value].to_i)
calc_values(row + 1, value + 1,triangle, results, current_path + triangle[row][value].to_i)
end
end
results = []
calc_values(0,0,triangle,results)
p results.max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment