Class: WordleDecoder::WordPosition::LetterPosition

Inherits:
Object
  • Object
show all
Defined in:
lib/wordle_decoder/word_position.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, hint_char, hint_chars, answer_chars) ⇒ LetterPosition

Returns a new instance of LetterPosition.



117
118
119
120
121
122
123
# File 'lib/wordle_decoder/word_position.rb', line 117

def initialize(index, hint_char, hint_chars, answer_chars)
  @index = index
  @hint_char = hint_char
  @hint_chars = hint_chars
  @answer_chars = answer_chars
  @answer_char = @answer_chars[index]
end

Instance Attribute Details

#answer_charObject (readonly)

Returns the value of attribute answer_char.



125
126
127
# File 'lib/wordle_decoder/word_position.rb', line 125

def answer_char
  @answer_char
end

#hint_charObject (readonly)

Returns the value of attribute hint_char.



125
126
127
# File 'lib/wordle_decoder/word_position.rb', line 125

def hint_char
  @hint_char
end

#indexObject (readonly)

Returns the value of attribute index.



125
126
127
# File 'lib/wordle_decoder/word_position.rb', line 125

def index
  @index
end

Instance Method Details

#impossible_words(commonality) ⇒ Object



147
148
149
150
151
# File 'lib/wordle_decoder/word_position.rb', line 147

def impossible_words(commonality)
  words = WordSearch.chars_at_index(@answer_chars, @index, commonality)
  reject_possible_words_where_letter_appears_multiple_times_and_is_in_answer!(words)
  words
end

#potential_words(commonality) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wordle_decoder/word_position.rb', line 129

def potential_words(commonality)
  case hint_char
  when "g"
    WordSearch.char_at_index(@answer_char, @index, commonality)
  when "y"
    if @hint_chars.count("g") == 3
      must_be_char_index = @hint_chars.index.with_index { |h, i| h != "g" && i != @index }
      must_be_char = @answer_chars[must_be_char_index]
      WordSearch.char_at_index(must_be_char, @index, commonality)
    else
      # TODO: if yellow char is in the word as green already, and there's only one isntance of it
      # in final word, can assume it's not that char.
      chars = @answer_chars - [@answer_char]
      WordSearch.chars_at_index(chars, @index, commonality)
    end
  end
end