Sha256: 3f0a20b57ea14d284ee25e02148ef91fd2b2a4033b2dc5452faa6a84fe386752

Contents?: true

Size: 1010 Bytes

Versions: 2

Compression:

Stored size: 1010 Bytes

Contents

# frozen_string_literal: true

module Codebreaker
  module Entities
    class Processor
      MATCHED_DIGIT_CHAR = '+'
      UNMATCHED_DIGIT_CHAR = '-'
      WRONG_DIGIT_CHAR = ''

      attr_reader :guess, :code, :result

      def secret_code_proc(code, guess)
        @code = code.split('')
        @guess = guess.split('')
        answer = handle_matched_digits.join + handle_matched_digits_with_wrong_position.join
        (code.length - answer.length).times { answer << WRONG_DIGIT_CHAR }
        answer
      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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codebreaker_marian-0.3.1 lib/codebreaker_marian/entities/processor.rb
codebreaker_marian-0.3.0 lib/codebreaker_marian/entities/processor.rb