Class: Waylon::Wordle::Solver
- Inherits:
-
Object
- Object
- Waylon::Wordle::Solver
- Defined in:
- lib/waylon/wordle/solver.rb
Overview
Utility class for solving Wordle puzzles
Instance Attribute Summary collapse
-
#answer ⇒ Object
readonly
Returns the value of attribute answer.
-
#attempts ⇒ Object
Returns the value of attribute attempts.
-
#hits ⇒ Object
Returns the value of attribute hits.
-
#misses ⇒ Object
Returns the value of attribute misses.
-
#near_hits ⇒ Object
Returns the value of attribute near_hits.
-
#startword ⇒ Object
readonly
Returns the value of attribute startword.
Instance Method Summary collapse
- #find_potential_solution ⇒ Object
-
#initialize(startword) ⇒ Solver
constructor
A new instance of Solver.
- #last_attempted_word ⇒ Object
- #make_attempt(answer) ⇒ Object
- #solve_todays_wordle ⇒ Object
Constructor Details
#initialize(startword) ⇒ Solver
Returns a new instance of Solver.
10 11 12 13 14 15 16 17 |
# File 'lib/waylon/wordle/solver.rb', line 10 def initialize(startword) @startword = startword @attempts = [] @hits = [] @near_hits = [] @misses = [] @answer = Waylon::Wordle.for_today.chars end |
Instance Attribute Details
#answer ⇒ Object (readonly)
Returns the value of attribute answer.
7 8 9 |
# File 'lib/waylon/wordle/solver.rb', line 7 def answer @answer end |
#attempts ⇒ Object
Returns the value of attribute attempts.
8 9 10 |
# File 'lib/waylon/wordle/solver.rb', line 8 def attempts @attempts end |
#hits ⇒ Object
Returns the value of attribute hits.
8 9 10 |
# File 'lib/waylon/wordle/solver.rb', line 8 def hits @hits end |
#misses ⇒ Object
Returns the value of attribute misses.
8 9 10 |
# File 'lib/waylon/wordle/solver.rb', line 8 def misses @misses end |
#near_hits ⇒ Object
Returns the value of attribute near_hits.
8 9 10 |
# File 'lib/waylon/wordle/solver.rb', line 8 def near_hits @near_hits end |
#startword ⇒ Object (readonly)
Returns the value of attribute startword.
7 8 9 |
# File 'lib/waylon/wordle/solver.rb', line 7 def startword @startword end |
Instance Method Details
#find_potential_solution ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/waylon/wordle/solver.rb', line 43 def find_potential_solution potential_solutions = Waylon::Wordle.vocabulary.map(&:chars).reject { _1.intersect?(misses) } candidates = potential_solutions.select do |word| hits_match?(hits, word) && near_hits_match?(near_hits, word) end candidates.shuffle.max_by { _1.uniq.size } end |
#last_attempted_word ⇒ Object
53 |
# File 'lib/waylon/wordle/solver.rb', line 53 def last_attempted_word = attempts.last[:word] |
#make_attempt(answer) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/waylon/wordle/solver.rb', line 27 def make_attempt(answer) word = next_word attempt = { word: word.join, result: [] } word.each_with_index do |letter, index| if letter == answer[index] add_hit(letter, index, attempt) elsif answer.include?(letter) add_near_hit(letter, index, word, attempt) else add_miss(letter, attempt) end end attempt end |
#solve_todays_wordle ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/waylon/wordle/solver.rb', line 19 def solve_todays_wordle until attempts.any? && (last_attempted_word == answer.join || attempts.size == 6) attempts << make_attempt(answer) end last_attempted_word == answer.join end |