-
Notifications
You must be signed in to change notification settings - Fork 0
/
89.rb
31 lines (24 loc) · 784 Bytes
/
89.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require 'pry'
# a = []
# File.open('89.txt').map do |line|
# a << line.split(' ').map(&:to_i)
# end
# 0.upto a.length - 1 do |lev|
# 0.upto i.length - 1 do |col|
# a[lev - 1][col - 1] += a[lev][col], a[]
# end
# p sume
rows = File.open('89.txt').map do |line|
line.split(' ').map(&:to_i)
end
# Starting from the bottom row of the triangle, find the maximum of each pair of
# adjacent numbers and add it to their common ancestor. As we move up the
# triangle the maximum path value will propagate up, eventually setting the
# topmost value to the answer.
(rows.count - 1).downto(1) do |level|
(rows[level].count - 1).downto(1) do |column|
binding.pry
rows[level - 1][column - 1] += [rows[level][column], rows[level][column - 1]].max
end
end
puts rows[0][0]