secret
Created

  • Download Gist
sebastian_rabuini.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
#Code rebuilt replacing the magnets
 
def skip_tracks(arr, i)
start = inc = i % arr.size()
count = 1
while inc > 0 && count < arr.size() do
c = start
while (c + inc) % arr.size() != start
before = (c - inc) % arr.size()
arr[before], arr[c] = arr[c], arr[before]
count = count + 1
c = c + inc
end
count = count + 1
start = start - 1
end
end
 
#My new version
 
def skip_tracks(arr, i)
i.abs.times { i > 0 ? arr.push(arr.shift) : arr.unshift(arr.pop) }
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.