Skip to content

Instantly share code, notes, and snippets.

@henrik
Created August 1, 2012 18:29
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/3229544 to your computer and use it in GitHub Desktop.
Save henrik/3229544 to your computer and use it in GitHub Desktop.
Scheduler - cleaned up version of Evgeny Kluev's solution http://stackoverflow.com/a/11761192/6962
class Scheduler
def initialize
@schedule = []
@word = 0
end
def next_slot
if @schedule.empty?
slot = 0
else
bit = 32
while (((@word ^= bit) & bit) == 0) do
bit >>= 1;
end
slot = (@word * 60) / 64
end
@schedule << slot
@schedule.sort!
slot
end
def schedule
@schedule
end
end
scheduler = Scheduler.new
60.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