Sha256: 1c532fadc2be52626df67838495cf049f8b180bbe6cdcf08b706613bde99617e

Contents?: true

Size: 736 Bytes

Versions: 4

Compression:

Stored size: 736 Bytes

Contents

# frozen_string_literal: true

class Processor
  MATCHED_DIGIT_CHAR = '+'
  UNMATCHED_DIGIT_CHAR = '-'

  attr_reader :guess, :code, :result

  def secret_code_proc(code, guess)
    @code = code.split('')
    @guess = guess.split('')
    handle_matched_digits.join + handle_matched_digits_with_wrong_position.join
  end

  private

  def handle_matched_digits
    code.map.with_index do |_, index|
      next unless code[index] == guess[index]

      @guess[index], @code[index] = nil
      MATCHED_DIGIT_CHAR
    end
  end

  def handle_matched_digits_with_wrong_position
    guess.compact.map do |number|
      next unless @code.include?(number)

      @code.delete_at(code.index(number))
      UNMATCHED_DIGIT_CHAR
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
codebreaker-yeroshek-0.1.1 lib/app/entities/processor.rb
codebreaker_rg-0.1.2 lib/app/entities/processor.rb
codebreaker_rg-0.1.1 lib/app/entities/processor.rb
codebreaker_rg-0.1.0 lib/app/entities/processor.rb