1
Fork 0
advent-of-code-2021/puzzles/01_2/main.rb
2021-12-09 11:22:06 +01:00

37 lines
588 B
Ruby

def shift(arr)
arr[0] = arr[1]
arr[1] = arr[2]
arr[2] = arr[3]
arr[3] = []
end
def push(arr, val)
shift(arr)
if arr[1]
arr[1] << val
end
if arr[2]
arr[2] << val
end
arr[3] << val
end
def run(f)
increments = 0
buffer = []
f.each do |line|
measurement = Integer(line)
push(buffer, measurement)
if buffer[0] and buffer[1] and buffer[0].length() == 3 and buffer[1].length() == 3 and buffer[1].sum() > buffer[0].sum()
increments += 1
end
end
return increments
end