class Codebreaker::SecretCodeMatcher

Constants

DIGIT_MATCH_SYMBOL
EXACT_MATCH_SYMBOL

Public Class Methods

new(secret_code, guess_code) click to toggle source
# File lib/codebreaker/secret_code_matcher.rb, line 6
def initialize(secret_code, guess_code)
  @secret_code = secret_code.dup
  @guess_code = guess_code.dup
end

Public Instance Methods

call() click to toggle source
# File lib/codebreaker/secret_code_matcher.rb, line 11
def call
  exact_match + digit_match
end

Private Instance Methods

digit_match() click to toggle source
# File lib/codebreaker/secret_code_matcher.rb, line 27
def digit_match
  @guess_code.compact.each_with_object('') do |digit, result|
    found_index = @secret_code.index(digit)

    next unless found_index

    result << DIGIT_MATCH_SYMBOL
    @secret_code[found_index] = nil
  end
end
exact_match() click to toggle source
# File lib/codebreaker/secret_code_matcher.rb, line 17
def exact_match
  @guess_code.each_with_object('').with_index do |(digit, result), index|
    next unless digit == @secret_code[index]

    result << EXACT_MATCH_SYMBOL
    @guess_code[index] = nil
    @secret_code[index] = nil
  end
end