Revised day 1

This commit is contained in:
Andrew R. M 2021-12-10 11:28:46 -05:00
parent 1bfd6c462d
commit 729a8ff201

View File

@ -16,17 +16,33 @@ end
puts "Amount of measurements larger than before: #{larger_than_before}"
larger_than_previous_window = 0
previous_window = nil
window = []
inputs.each_with_index do |input, index|
unless index+2 >= inputs.size # Handle the last window not overreaching
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
# A struct to represent a three item sliding window
Window = Struct.new(:a, :b, :c) do
include Comparable
def <=>(other)
self.a + self.b + self.c <=> other.a + other.b + other.c
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}"