Skip to content

Instantly share code, notes, and snippets.

@RyanTG
Created August 4, 2022 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanTG/1ce4c3b3de43aaaebb0ba19059cc0174 to your computer and use it in GitHub Desktop.
Save RyanTG/1ce4c3b3de43aaaebb0ba19059cc0174 to your computer and use it in GitHub Desktop.
Max overlapping ranges
#!/usr/bin/env ruby
require 'csv'
require 'time'
class PeakVehicleReq
def initialize(filename)
@filename = 'Trips.csv'
end
def process
table = parse_file
run_scenarios(table)
output_results(table)
end
private
def parse_file
CSV.parse(File.read(@filename), headers: true)
end
def run_scenarios(table)
table.each do |row|
row["count"] = 1
counter = 0
current_begin = row["Begin"].to_f
current_end = row["End"].to_f
division = row["Division"]
table.each do |row|
if current_begin >= row["Begin"].to_f && current_begin <= row["End"].to_f && division == row["Division"]
counter += 1
end
end
row["count"] = counter
end
end
def output_results(table)
CSV.open(output_filename, 'w', headers: table.headers, write_headers: true) do |csv|
table.each { |row| csv << row }
end
end
def output_filename
File.join(
File.dirname(@filename),
"#{File.basename(@filename, '.csv')}_PVR.csv"
)
end
end
PeakVehicleReq.new(ARGV[0]).process if __FILE__ == $PROGRAM_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment