diff --git a/lib/methods.rb b/lib/methods.rb new file mode 100644 index 0000000..30ea528 --- /dev/null +++ b/lib/methods.rb @@ -0,0 +1,43 @@ +require 'csv' + +def qube_partner_cinema + CSV.parse(File.read('partners.csv'), :headers=>true, header_converters: lambda {|f| f.strip}, converters: lambda {|f| f ? f.strip : ''}) +end + +def qube_cinema_input_data + input_data = CSV.parse(File.read('input.csv')).sort_by {|row| row[1].to_i}.reverse +end + +def qube_cinema_content_size(row, delivery_content_size) + row["Size Slab (in GB)"].split('-').first.to_i <= delivery_content_size && delivery_content_size <= row["Size Slab (in GB)"].split('-').last.to_i +end + +def qube_cinema_calculate_delivery_cost(partner_row, qube_cinema_delivery_content_size) + cost_per_rate = qube_cinema_delivery_content_size * partner_row['Cost Per GB'].to_i + partner_minimum_cost = partner_row['Minimum cost'].to_i + cost = cost_per_rate < partner_minimum_cost ? partner_minimum_cost : cost_per_rate +end + +def qube_cinema_find_eligible_delivery_partners(delivery_theatre, delivery_content_size) + eligible_delivery_partners = qube_partner_cinema.select{|row| row['Theatre'] == delivery_theatre && qube_cinema_content_size(row, delivery_content_size)} +end + +def qube_cinema_capacities_data + CSV.parse(File.read("capacities.csv"), headers: true, header_converters: lambda {|f| f.strip}, converters: lambda {|f| f ? f.strip : nil}) +end + +def qube_cinema_partner_capacity?(partner_id, delivery_content_size) + partner_total_capacity = qube_cinema_capacities_data.find { |row| row['Partner ID'] == partner_id }['Capacity (in GB)'].to_i + delivery_content_size <= (partner_total_capacity - $partner_allocated_capacities[partner_id]) +end + +def generationg_qube_output_for_cinema(output_qube) + output_qube.each.with_index(1) do | element, index | + CSV.open("output#{index}.csv", "w") do |csv| + element.sort_by {|row| row[0]}.each do |row| + csv << row + end + end + puts "Qube Cinema content 'output#{index}.csv'" + end +end \ No newline at end of file diff --git a/solution.rb b/solution.rb new file mode 100644 index 0000000..02871c4 --- /dev/null +++ b/solution.rb @@ -0,0 +1,50 @@ +load 'lib/methods.rb' +def qube_cinema_solution + qube_partner_cinema + qube_cinema_input_data + output1 = [] + output2 = [] + final_output = [] + $partner_allocated_capacities = Hash.new(0) + qube_cinema_input_data.each do |input_row| + delivery_id = input_row[0] + delivery_content_size = input_row[1].to_i + delivery_theatre = input_row[2] + eligible_delivery_partners = qube_cinema_find_eligible_delivery_partners(delivery_theatre, delivery_content_size) + costs = [] + if eligible_delivery_partners.length != 0 + eligible_delivery_partners.each do |partner_row| + costs << qube_cinema_calculate_delivery_cost(partner_row, delivery_content_size) + end + minimum_cost = costs.min + minimum_cost_index = costs.index(minimum_cost) + cheapest_partner = eligible_delivery_partners[minimum_cost_index] + output1 << [delivery_id, true, cheapest_partner['Partner ID'], minimum_cost] + until qube_cinema_partner_capacity?(cheapest_partner['Partner ID'], delivery_content_size) + costs.delete_at(minimum_cost_index) + eligible_delivery_partners.delete_at(minimum_cost_index) + break if eligible_delivery_partners.empty? + minimum_cost = costs.min + minimum_cost_index = costs.index(minimum_cost) + cheapest_partner = eligible_delivery_partners[minimum_cost_index] + end + if eligible_delivery_partners.empty? + output2 << [delivery_id, false, "", ""] + else + output2 << [delivery_id, true, cheapest_partner['Partner ID'], minimum_cost] + $partner_allocated_capacities[cheapest_partner['Partner ID']] += minimum_cost + end + else + + output1 << [delivery_id, false, "", ""] + output2 << [delivery_id, false, "", ""] + + end + end + final_output << output1 << output2 + generationg_qube_output_for_cinema(final_output) + + +end + +qube_cinema_solution \ No newline at end of file