Skip to content

Instantly share code, notes, and snippets.

@wiegand
Created June 5, 2012 16:46
Show Gist options
  • Save wiegand/2876168 to your computer and use it in GitHub Desktop.
Save wiegand/2876168 to your computer and use it in GitHub Desktop.
Problem #67
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