Class: DigitsSolver::SolutionSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/digits_solver/solution_set.rb

Constant Summary collapse

DEFAULT_STRATEGY =
DigitsSolver::Strategies::BruteForce

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/digits_solver/solution_set.rb', line 8

def count
  @count
end

#problem_statementObject (readonly)

Returns the value of attribute problem_statement.



8
9
10
# File 'lib/digits_solver/solution_set.rb', line 8

def problem_statement
  @problem_statement
end

#strategyObject (readonly)

Returns the value of attribute strategy.



8
9
10
# File 'lib/digits_solver/solution_set.rb', line 8

def strategy
  @strategy
end

Class Method Details

.solve_for(problem_statement, strategy: DEFAULT_STRATEGY) ⇒ Object



11
12
13
14
15
16
# File 'lib/digits_solver/solution_set.rb', line 11

def solve_for(problem_statement, strategy: DEFAULT_STRATEGY)
  extend strategy
  solutions, attempts_count = solve(problem_statement)
  DigitsSolver.logger.info "Found #{solutions.count} potentially non unique solutions amongst #{attempts_count} tries."
  new problem_statement, solutions, strategy
end

Instance Method Details

#best_solution(nb = 1) ⇒ Object Also known as: best_solutions



30
31
32
33
34
35
36
37
38
# File 'lib/digits_solver/solution_set.rb', line 30

def best_solution(nb = 1)
  res = indexed_solutions.keys
                         .sort { |a, b| a.size <=> b.size }
                         .take(nb)
                         .map { |k| indexed_solutions[k] }
                         .flatten
                         .take(nb)
  nb == 1 ? res.first : res
end

#each(&block) ⇒ Object



19
20
21
# File 'lib/digits_solver/solution_set.rb', line 19

def each(&block)
  solutions.each(&block)
end

#sizeObject



42
43
44
# File 'lib/digits_solver/solution_set.rb', line 42

def size
  solutions.size
end

#sorted_solutionsObject



23
24
25
26
27
28
# File 'lib/digits_solver/solution_set.rb', line 23

def sorted_solutions
  indexed_solutions.keys
                   .sort { |a, b| a.size <=> b.size }
                   .map { |k| indexed_solutions[k] }
                   .flatten
end