Created
August 1, 2012 14:32
-
-
Save henrik/3227346 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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