Class: CodewordsSolver

Inherits:
Object
  • Object
show all
Defined in:
lib/codewords_solver.rb,
lib/codewords_solver/version.rb,
lib/codewords_solver/dictionary.rb

Defined Under Namespace

Classes: Dictionary

Constant Summary collapse

MAX_LOOP_ATTEMPTS =
5
VERSION =
"0.0.3".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coded_words, starting_letters, debug: false) ⇒ CodewordsSolver



10
11
12
13
14
15
16
17
18
# File 'lib/codewords_solver.rb', line 10

def initialize(coded_words, starting_letters, debug: false)
  @coded_words = coded_words
  @starting_letters = starting_letters
  @letters_by_number = (1..26).to_h { |n| [n, nil] }.merge(
    starting_letters.transform_values(&:upcase)
  )
  @dictionary = Dictionary.new
  @debug = debug
end

Class Method Details

.solve!(coded_words:, starting_letters:, **rest) ⇒ Object



4
5
6
# File 'lib/codewords_solver.rb', line 4

def self.solve!(coded_words:, starting_letters:, **rest)
  new(coded_words, starting_letters, **rest).solve!
end

Instance Method Details

#solve!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/codewords_solver.rb', line 20

def solve!
  num_attempts = 0

  until complete? || num_attempts == MAX_LOOP_ATTEMPTS
    # puts "Doing a loop (attempts so far: #{num_attempts})"
    do_loop
    num_attempts += 1
  end

  if complete?
    puts "PUZZLE IS COMPLETE:"

    puts

    print_words

    puts
    return
  end

  print_answer

  raise "Could not complete puzzle in #{num_attempts} (max attempts: #{MAX_LOOP_ATTEMPTS})"
end