Revised day 1
This commit is contained in:
parent
1bfd6c462d
commit
729a8ff201
@ -16,17 +16,33 @@ end
|
|||||||
|
|
||||||
puts "Amount of measurements larger than before: #{larger_than_before}"
|
puts "Amount of measurements larger than before: #{larger_than_before}"
|
||||||
|
|
||||||
larger_than_previous_window = 0
|
# A struct to represent a three item sliding window
|
||||||
previous_window = nil
|
Window = Struct.new(:a, :b, :c) do
|
||||||
window = []
|
include Comparable
|
||||||
inputs.each_with_index do |input, index|
|
def <=>(other)
|
||||||
unless index+2 >= inputs.size # Handle the last window not overreaching
|
self.a + self.b + self.c <=> other.a + other.b + other.c
|
||||||
window = [inputs[index], inputs[index+1], inputs[index+2]]
|
|
||||||
if previous_window && window.reduce(:+) > previous_window.reduce(:+)
|
|
||||||
larger_than_previous_window += 1
|
|
||||||
end
|
|
||||||
previous_window = window
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Take a list of inputs and translate them into three section windows
|
||||||
|
def windowfy(inputs)
|
||||||
|
windowfied_inputs = []
|
||||||
|
inputs.each_with_index do |_, index|
|
||||||
|
unless index + 2 >= inputs.size
|
||||||
|
window = Window.new(inputs[index], inputs[index+1], inputs[index+2])
|
||||||
|
windowfied_inputs << window
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return windowfied_inputs
|
||||||
|
end
|
||||||
|
|
||||||
|
windowfied_inputs = windowfy(inputs)
|
||||||
|
|
||||||
|
larger_than_previous_window = 0
|
||||||
|
previous_window = nil
|
||||||
|
windowfied_inputs.each do |window|
|
||||||
|
larger_than_previous_window += 1 if previous_window && window > previous_window
|
||||||
|
previous_window = window
|
||||||
|
end
|
||||||
|
|
||||||
puts "Amount of measurements larger than preivous window: #{larger_than_previous_window}"
|
puts "Amount of measurements larger than preivous window: #{larger_than_previous_window}"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user