ruby solution with orbits and permutations