blob: 1e0396dcefd398ee725e0681226dbaa1bf55b659 (
plain)
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
32
33
34
35
|
#!/usr/bin/ruby
#
# See ../README.md
#
#
# Run as: ruby ch-2.rb < input-file
#
numbers = []
#
# Parse the data: iterate over the lines of input, split each
# line on whitespace, turn each item into an integer, and
# add the split elements as a row of numbers to the array numbers.
#
ARGF . each_line do |_|
numbers << _ . split . map {|_| _ . to_i}
end
#
# Calculate the minimum path, bottom to top
#
(numbers . length - 2) . downto (0) do |x|
(0) . upto (numbers [x] . length - 1) do |y|
numbers [x] [y] += [numbers [x + 1] [y],
numbers [x + 1] [y + 1]] . min
end
end
#
# Print result
#
puts numbers [0] [0]
|