public
Last active

rpcfn6. Fails one test. Quick, though. "Good enough", not "perfect".

  • Download Gist
rpcfn6.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
class Array
# Ruby shortcut to quickly sum everything.
# This will obviously break if the elements can't be added together.
def sum
self.inject(&:+)
end
end
 
class Press < Array
include Enumerable, Comparable
attr_accessor :jobs, :time
 
def initialize jobs=[], time=0
@jobs = jobs
@time = time
end
 
def << *jobs
@jobs.concat jobs.flatten
@time += jobs.sum
self
end
 
def each
@jobs.each { |job| yield job }
end
 
def <=> other
@time <=> other.time
end
 
def inspect
@jobs
end
 
def delete *items
items.flatten.each do |item|
if @jobs.include? item
@jobs.delete item
@time -= item
end
end
self
end
 
def clear
@jobs = []
@time = 0
self
end
end
 
class FairDistribution
attr_reader :time_required, :distribution
 
def initialize jobs=[], presses=0
puts
throw "Stop the presses! They've been stolen!" if presses.zero?
@count_of_presses = presses
@jobs = jobs.sort
@time_required = 0
@distribution = []
mills_mess
populate_time_and_distribution # Get press queues and longest running time.
 
end
 
private
 
# The hardest three-ball juggling sequence there is!
def mills_mess
if @count_of_presses == 1
press = Press.new
press << @jobs
@distribution << press.jobs
@time_required = press.time
return
end
 
@presses = Array.new(@count_of_presses) { Press.new }
 
@presses.min << @jobs.pop until @jobs.empty?
 
 
end
 
def populate_time_and_distribution
@presses.each { |press| @distribution << press.jobs }
@time_required = @presses.max.time
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.