62 lines
1.6 KiB
Crystal
62 lines
1.6 KiB
Crystal
|
|
class Bordle
|
|
class TargetWord
|
|
def initialize(@target_word : String)
|
|
end
|
|
|
|
def equals?(word)
|
|
@target_word == word
|
|
end
|
|
|
|
def to_h()
|
|
hash = Hash(Char, Array(Int32)).new
|
|
@target_word.each_char_with_index do |char, index|
|
|
hash[char] = [] of Int32 unless hash.has_key? char
|
|
hash[char] << index
|
|
end
|
|
hash
|
|
end
|
|
|
|
def diff(word)
|
|
hash = self.to_h
|
|
result = [] of LetterScore
|
|
|
|
# REF = r a t e s
|
|
# TEST= c e r e t
|
|
|
|
word.each_char_with_index do |char, index|
|
|
if ! hash.has_key? char
|
|
result << {char, Score::NotOk}
|
|
next
|
|
end
|
|
|
|
char_values = hash[char]
|
|
char_misplaced = char_values.select {|pos| @target_word[pos] != word[pos] }
|
|
char_wellplaced = char_values.select {|pos| @target_word[pos] == word[pos] }
|
|
# puts "values(#{char}) = #{char_values}"
|
|
# puts "misplaced(#{char}) = #{char_misplaced}"
|
|
# puts "wellplaced(#{char}) = #{char_wellplaced}"
|
|
|
|
if char_wellplaced.includes? index
|
|
result << {char, Score::RightPlace}
|
|
char_wellplaced.reject! {|pos| pos == index }
|
|
hash[char] = char_misplaced + char_wellplaced
|
|
hash.delete(char) if hash[char].empty?
|
|
next
|
|
end
|
|
|
|
if ! char_misplaced.empty?
|
|
result << {char, Score::WrongPlace}
|
|
char_misplaced = char_misplaced.skip(1)
|
|
hash[char] = char_misplaced + char_wellplaced
|
|
hash.delete(char) if hash[char].empty?
|
|
next
|
|
end
|
|
|
|
result << {char, Score::NotOk}
|
|
end
|
|
result
|
|
end
|
|
end
|
|
end
|