Created
June 5, 2012 16:46
-
-
Save wiegand/2876168 to your computer and use it in GitHub Desktop.
Problem #67
This file contains 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
tri = File.open('triangle.txt').map do |row| | |
row.split.map {|i| i.to_i} | |
end | |
tri.to_enum.with_index do |row, ri| | |
row.to_enum.with_index do |n, ni| | |
pl = (ri == 0 || ni == 0) ? 0 : tri[ri-1][ni-1] | |
pr = (ri == 0 || (ni + 1) == row.length) ? 0 : tri[ri-1][ni] | |
p_max = [pl, pr].max | |
row[ni] = row[ni] + p_max | |
end | |
end | |
puts tri.last.max |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment