[Solved] Counting changes in a nested array


result =
  input.map do |k, t|
    [k, DateTime.parse(t)]
  end.each_with_object(prev: nil, count: 0) do |(k, t), acc|
    case k
                  # keep the time of the previous occurrence of "red" 
    when "red" then acc[:prev] = t    
    when "green" 
                        # seconds 
      acc[:count] += 1 if 24.0 * 60 * 60 * (t - acc[:prev]) < 10
      acc[:prev] = nil  
    end  
  end[:count]

  p(result)
  #⇒ 2

8

solved Counting changes in a nested array