public
Created

  • Download Gist
scheduler.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
class Scheduler
def initialize(slot_count=60)
@schedule = []
@slot_count = slot_count
end
 
def next_slot
# Start a new schedule on each lap around the slots,
# so slot 60 is filled the same as slot 0.
if (@schedule.length % @slot_count).zero?
@lap = []
end
 
if @lap.empty?
slot = 0
else
circle = @lap + [@lap.first + @slot_count]
slot = 0
largest_known_distance = 0
 
circle.each_cons(2) do |(from, unto)|
distance = (from - unto).abs
if distance > largest_known_distance
largest_known_distance = distance
slot = (from + distance/2) % @slot_count
end
end
end
 
@schedule << slot
@lap << slot
@schedule.sort!
@lap.sort!
 
slot
end
 
def schedule
@schedule
end
end
 
 
scheduler = Scheduler.new
 
62.times do
scheduler.next_slot
p scheduler.schedule
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.