Class: Waylon::Wordle::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/waylon/wordle/solver.rb

Overview

Utility class for solving Wordle puzzles

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#answerObject (readonly)

Returns the value of attribute answer.



7
8
9
# File 'lib/waylon/wordle/solver.rb', line 7

def answer
  @answer
end

#attemptsObject

Returns the value of attribute attempts.



8
9
10
# File 'lib/waylon/wordle/solver.rb', line 8

def attempts
  @attempts
end

#hitsObject

Returns the value of attribute hits.



8
9
10
# File 'lib/waylon/wordle/solver.rb', line 8

def hits
  @hits
end

#missesObject

Returns the value of attribute misses.



8
9
10
# File 'lib/waylon/wordle/solver.rb', line 8

def misses
  @misses
end

#near_hitsObject

Returns the value of attribute near_hits.



8
9
10
# File 'lib/waylon/wordle/solver.rb', line 8

def near_hits
  @near_hits
end

#startwordObject (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_solutionObject



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_wordObject



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_wordleObject



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