Skip to content

Instantly share code, notes, and snippets.

@henrik
Created August 1, 2012 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/3227346 to your computer and use it in GitHub Desktop.
Save henrik/3227346 to your computer and use it in GitHub Desktop.
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