#!/usr/bin/ruby
=begin
Find the maximum total from top to bottom of the triangle below:
NOTE: As there are only 16384 routes, it is possible to solve this problem by
trying every route. However, Problem 67, is the same challenge with a triangle
containing one-hundred rows; it cannot be solved by brute force, and requires
a clever method! ;o)
=end
text =
"75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"
# convert this text into an array of Fixnum arrays (this is Ruby)
rows = text.split("\n").map {|r| r.split(' ').map {|n| n.to_i } }.reverse
# start at the bottom of the pyramid and reduce it to a max
max = rows.reduce([0] * rows[0].length) do |maxes, row|
if row.length == 1
# maybe I shouldn't have used reduce
row[0] + maxes[0]
else
# add the elements of the maxes array to rows
0.upto(row.length - 1) do |i|
row[i] += maxes[i]
end
temp = []
# make the next maxes array from pairs of
0.upto(row.length - 2) {|i| temp += [row[i, 2].max] }
temp
end
end
puts max
Refactorings
No refactoring yet !
darkleo.myopenid.com
May 24, 2010, May 24, 2010 10:39, permalink
Range#map ?
# Read data and convert it
file = File.open("18.txt", 'r')
data = []
file.each_line {|line|
data << line.split(/ /).map {|i| i.to_i}}
file.close
n = data.size
(1..n).map {|i|
(2..data[n-i].size).map {|k|
data[n-i-1][k-2] += data[n-i][k-2, 2].max}}
p data[0][0]
arvanasse
May 25, 2010, May 25, 2010 15:42, permalink
Here's a first cut just making it more "genuinely ruby"...
text =
"75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"
# convert this text into an array of Fixnum arrays (this is Ruby)
rows = text.split("\n").map {|r| r.split(' ').map {|n| n.to_i } }.reverse
# start at the bottom of the pyramid and reduce it to a max
max = rows.reduce([0] * rows.first.length) do |maxes, row|
if row.length == 1
# maybe I shouldn't have used reduce
row.first + maxes.first
else
# add the elements of the maxes array to rows
maxes.each_with_index{|max, i| row[i] += max }
# make the next maxes array from pairs of
row[0, row.length-1].zip( row[1, row.length-1] ).map(&:max)
end
end
puts max
arvanasse
May 25, 2010, May 25, 2010 15:56, permalink
The 'row.length == 1' test simply changes what's returned from the block so it can be transformed into a simpler control in the final statement...
text =
"75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"
# convert this text into an array of Fixnum arrays (this is Ruby)
rows = text.split("\n").map {|r| r.split(' ').map {|n| n.to_i } }.reverse
# start at the bottom of the pyramid and reduce it to a max
max = rows.reduce([0] * rows.first.length) do |maxes, row|
# add the elements of the maxes array to rows
maxes.each_with_index{|max, i| row[i] += max }
# make the next maxes array from pairs of
row.length > 1 ? row[0, row.length-1].zip( row[1, row.length-1] ).map(&:max) : row
end
puts max
Martin Plöger
May 27, 2010, May 27, 2010 18:05, permalink
text =
"75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"
# convert this text into an array of Fixnum arrays (this is Ruby)
rows = text.lines.map { |line| line.split.map &:to_i }.reverse
# start at the bottom of the pyramid and reduce it to a max
# empty Array is just fine, because the 1st row is not "maxed" anyway
max = rows.inject [] do |maxes, row|
# add the elements of the maxes array to rows
maxes.each.with_index { |max, index| row[index] += max }
# make the next maxes array from pairs of
row.length > 1 ? row.each_cons(2).map(&:max) : row
end
puts max
lordzoner.myopenid.com
May 28, 2010, May 28, 2010 14:50, permalink
Thanks everyone. I was really tired when I posted this, and had totally forgotten about Enumerable#map. Appreciate the help.
I'm working on Project Euler, and this is my algorithm for Problem 18 (and 67). It produces the correct answer, but I've hit a mental block making it Ruby-like. What improvements can I make to this to make it more genuinely Ruby?