wordle_decoder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "wordle_decoder/version"
4
+ require_relative "wordle_decoder/word_search"
5
+ require_relative "wordle_decoder/word_position"
6
+ require_relative "wordle_decoder/word"
7
+ require_relative "wordle_decoder/guess"
8
+ require_relative "wordle_decoder/wordle_share"
9
+
10
+ class WordleDecoder
11
+ class Error < StandardError; end
12
+
13
+ def initialize(wordle_share)
14
+ @wordle_share = wordle_share
15
+ end
16
+
17
+ def to_terminal
18
+ @to_terminal ||= format_to_terminal
19
+ end
20
+
21
+ def best_guess
22
+ @best_guess ||= guesses.first
23
+ end
24
+
25
+ def guesses
26
+ @guesses ||= initialize_and_sort_guesses
27
+ end
28
+
29
+ def word_positions
30
+ @word_positions ||= initialize_word_positions
31
+ end
32
+
33
+ def inspect
34
+ lines = ["<#{self.class.name} "]
35
+ lines << @wordle_share.inspect
36
+ lines.concat guesses.map(&:inspect)
37
+ lines.last << ">"
38
+ lines.join("\n")
39
+ end
40
+
41
+ private
42
+
43
+ def initialize_and_sort_guesses
44
+ first_word_position, *remaining_positions = word_positions.reverse
45
+ guesses = first_word_position.potential_words.map do |start_word|
46
+ Guess.new(start_word, first_word_position, remaining_positions)
47
+ end
48
+ guesses.sort_by!(&:score)
49
+ guesses.reverse!
50
+ guesses
51
+ end
52
+
53
+ def initialize_word_positions
54
+ @wordle_share.hint_lines.map.with_index do |line, index|
55
+ WordPosition.new(line, index, @wordle_share.answer_chars)
56
+ end
57
+ end
58
+
59
+ def format_to_terminal
60
+ str = +"\n"
61
+ best_guess.words_with_scores.reverse_each do |word, guess_score|
62
+ str << " #{word.to_terminal} #{word.confidence_score(guess_score)}\n"
63
+ end
64
+ str << " {{green:#{@wordle_share.answer}}}\n\n"
65
+ end
66
+ end