-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.rb
79 lines (73 loc) · 2.37 KB
/
18.rb
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'pry'
require 'pry'
require './lib/timer'
# https://projecteuler.net/problem=18
timer = Timer.new
timer.start_timer
# set up the array of arrays called "triangle", correctly spaced using nils
# flip the triangle over so it's widest on 'top' or at index 0
file = File.open("./18.txt", "r")
arrays = []
file.each do |line|
row = []
line.delete("\n")
row = line.split(" ")
arrays.unshift(row)
end
arrays.each do |row|
row.map!{|number| number.to_i}
end
triangle = []
arrays.each_with_index do |array,index|
row = []
index.times do row.push(nil) end
array.each_with_index do |number, index|
row.push(number)
unless index == array.size - 1
row.push(nil)
end
end
index.times do row.push(nil) end
triangle.push(row)
end
# evaluation...
sums = []
triangle.each_with_index do |triangle_row, triangle_index|
if triangle_index == 0
# skip
elsif triangle_index == 1
# for the second to longest row, look at row above in the triangle array for the largest, adjacent (diagonally) number. Then sum that and add to the row_sums array.
row_sums = []
triangle_row.each_with_index do |row_value, row_index|
unless row_value == nil
if triangle[triangle_index - 1][row_index - 1] > triangle[triangle_index - 1][row_index + 1]
row_sums.push(row_value + triangle[triangle_index - 1][row_index - 1])
else
row_sums.push(row_value + triangle[triangle_index - 1][row_index + 1])
end
end
end
# after doing for all values in a row, push the row_sums to the sums array.
sums.push(row_sums)
else
# for each row after the second, refer to the sums array to check for which diagonal number is largest. Then, again, add to the row_sums array and then push that to the end of the sums array.
row_sums = []
triangle_row.delete(nil)
triangle_row.each_with_index do |row_value, row_index|
if sums[-1][row_index] > sums[-1][row_index + 1]
row_sums.push(row_value + sums[-1][row_index])
else
row_sums.push(row_value + sums[-1][row_index + 1])
end
end
sums.push(row_sums)
end
# after doing for each row, the last array in the sums array will be the value of the maximum sum path.
if triangle_index == triangle.size - 1
puts sums[-1]
end
end
timer.stop_timer
timer.calculate_duration
### STATS
# Duration: 0.33 milliseconds.