Triangle Problem

Starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

   3
  7 4
 2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below: …–

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
class Triangle

  def initialize (file)
    @file = file
  end

  def get_array
    File.open(@file, 'r') do |f|
      f.each_line.collect do |line|
        line.split(' ').collect { |num| num.to_i }
      end
    end
  end

  def calculate
    result = get_array.reverse.inject do |sum_line, line|
      line.collect.with_index do |num, i|
        num + [sum_line[i], sum_line[i + 1]].max
      end
    end
    result[0]
  end
end

puts Triangle.new('triangle.txt').calculate
Comments

Comments