-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.rb
More file actions
52 lines (41 loc) · 952 Bytes
/
script.rb
File metadata and controls
52 lines (41 loc) · 952 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Script
def initialize
calculating
end
private
def reading
input = File.open("input.txt") {|file| file.read}
i = 0
input_array = []
input.split("\n").each do |str|
input_array[i] = str.split(' ').collect! {|c| c.to_i}
i += 1
end
input_array
end
def make_path(hill)
path = [0]
y = 1
path << hill[y].index(hill[y].max)
y = 2
while y < hill.count do
path << hill[y].index([hill[y][path[y-1]],hill[y][path[y-1]+1]].max)
y += 1
end
path
end
def calculating
hill_array = reading
x = y = hill_array.count - 1
while x != 0 || y != 0 do
hill_array[y-1][x-1] =
hill_array[y-1][x-1] +
[hill_array[y][x], hill_array[y][x-1]].max unless x.zero?
x = x.zero? ? y = y - 1 : x - 1
end
puts "Max sum: #{hill_array[0][0]}"
puts "Indexes of optimal path:"
p make_path(hill_array)
end
end
Script.new