class Challenge::Solution

Public Class Methods

new(theatres_file, capacities_file, partners_file) click to toggle source
# File lib/challenge.rb, line 7
def initialize(theatres_file, capacities_file, partners_file)
        @theatres = import_theatres_data(theatres_file)
        @capacities = import_capacities_data(capacities_file)
        @partners = import_partners_data(partners_file)
        economical_deliveries
        distributed_deliveries
end

Private Instance Methods

distributed_deliveries() click to toggle source
# File lib/challenge.rb, line 21
def distributed_deliveries    
        delivery_options = @theatres.map { |theatre| theatre.deliverable_options  }.select{ |theatre| theatre.size > 0 }
        possible_delivery_options = delivery_options&.first.product(*delivery_options.drop(1))
        deliverable = {}
        deliverable_cost = nil
        possible_delivery_options.each do |possible_delivery_option|
                definded_capacities = @capacities.dup
                feasible_option = {}
                possible_cost = 0
                possible_delivery_option.each do |delivery_option|          
                        if definded_capacities[delivery_option.partner_id] >= delivery_option.delivery_size
                                feasible_option[delivery_option.delivery_id] = delivery_option
                                definded_capacities[delivery_option.partner_id] -= delivery_option.delivery_size
                                possible_cost += delivery_option.delivery_cost
                        else               
                                feasible_option = deliverable.dup
                                possible_cost = deliverable_cost
                                break
                        end
                end
                if deliverable_cost.nil? || deliverable_cost > possible_cost
                        deliverable_cost = possible_cost
                        deliverable = feasible_option
                end
        end
        output2 = @theatres.map { |theatre| deliverable[theatre.delivery_id].nil? ? Deliverable.new(theatre.delivery_id, false) : deliverable[theatre.delivery_id]  }
        export_data('output2.csv', output2)
end
economical_deliveries() click to toggle source
# File lib/challenge.rb, line 16
def economical_deliveries             
        output1 = @theatres.each { |theatre| theatre.set_deliverable_options(@partners) }.map { |deliverable| deliverable.filter_by_cost  }  
        export_data('output1.csv', output1)
end