Sha256: 361bd3edc02dd087af78548f3a7f8fb91fa30d21da4aed1aedfec989f873f4bb

Contents?: true

Size: 705 Bytes

Versions: 1

Compression:

Stored size: 705 Bytes

Contents

# frozen_string_literal: true
#
# We want a unique map for each place in the original number
module ScatterSwap
  class Swapper
    @all_swapper_maps = {}

    class << self
      def instance(spin)
        @all_swapper_maps[spin] ||= new(spin)
      end
    end

    DIGITS = (0..9).to_a.freeze

    def initialize(spin)
      @spin = spin
      @caches = {}
    end

    def generate(index)
      @caches[index] ||= generate_without_cache(index)
    end

    private

    def generate_without_cache(index)
      array = DIGITS.dup
      sum = 0
      10.times.map.with_index do |i|
        sum = (sum + ((index + i) ^ @spin) - 1) % (10 - i)
        next array.delete_at(sum)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bonio-scatter_swap-1.0.0 lib/scatter_swap/swapper.rb