Done puzzle 64
[project-euler.git] / euler67.rb
1 require 'logger'
2
3 $triangle = File.readlines('euler67-triangle.txt').map {|line| line.chomp.split.map {|s| s.to_i}}
4
5 def find_best(triangle)
6 log = Logger.new(STDERR)
7 log.level = Logger::WARN
8 last_line_totals = [0]
9 this_line_totals = []
10 triangle.each do |line|
11 log.info {"Checking line #{line}"}
12 this_line_totals = []
13 line.each_index do |i|
14 this_line_totals << [i == 0 ? 0 : last_line_totals[i-1] + line[i],
15 i == last_line_totals.length ? 0 : last_line_totals[i] + line[i]].max
16 end
17 log.info {"Best totals for line #{line} are #{this_line_totals}"}
18 last_line_totals = this_line_totals
19 end
20 log.close
21 this_line_totals.max
22 end
23
24 if __FILE__==$0
25 puts find_best $triangle
26 end