Sha256: 08236959de0a6b2f3f8e794ec352e7a3b174fec891130d39d49e22430ca5574f

Contents?: true

Size: 803 Bytes

Versions: 3

Compression:

Stored size: 803 Bytes

Contents

class CodeFormatter
  def self.call(*args)
    new(*args).call
  end

  def initialize(maker_code = [], user_code = [])
    @maker_code = maker_code
    @user_code = user_code
  end

  def call
    format(@maker_code, @user_code)
  end

  def format(maker_code, user_code)
    user_code.each_with_index.map do |char, index|
      if maker_code[index] == user_code[index]
        maker_code[index] = nil
        '+'
      else
        char_index = maker_code.index(char)
        if char_index && minus?(char_index, maker_code, user_code)
          maker_code[char_index] = nil
          '-'
        end
      end
    end.compact.sort
  end

  private

  def minus?(char_index, maker_code, user_code)
    return unless maker_code[char_index]

    maker_code[char_index] != user_code[char_index]
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
codebreaker-engine-0.1.1 lib/codebreaker/code_formatter.rb
codebreaker-engine-0.1.0 lib/codebreaker/code_formatter.rb
codebreaker-1.0.0-0.1.0 lib/codebreaker/code_formatter.rb