Skip to content

@henrik /scheduler.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.