digits_solver 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cc76291aaef108bc1e004bb3477fddd31520a414a82027be8111ef746add465
|
4
|
+
data.tar.gz: '08302397fba6f31c6c92e8a14e9817f8b2f308c7d0fb2c1e46b17b653a11d20c'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd79298b33da986c95ad772b18c3cbad3c8827b2f7e3ab5c76a07fe6d17abb1247000c3d172e557ee78ebcef98bd71df73983348271fe76da0ecea5b240b7fb8
|
7
|
+
data.tar.gz: 7fe1e62a96ce0d064628857740f46e1b4b2d54e88a4988581d0524f614a3bf84a11130d9ad589a74fdfb1158a18a5b80f101d3d2f559dcc8d525e988a19367c3
|
data/README.md
CHANGED
@@ -6,11 +6,11 @@ The goal of this gem is to solve puzzles à-la [NY Times Digits game](https://ww
|
|
6
6
|
actually know this game under a different name. In France this looks like the calculation part of an almost 50 year old TV
|
7
7
|
show named 'Les chiffres et les lettres'...
|
8
8
|
|
9
|
-
The goal is to use numbers from the draw combined by any basic operation (+ - / *) to reach that goal. You cannot have negative numbers as result of "-" operation, and can only divide number resulting
|
9
|
+
The goal is to use numbers from the draw combined by any basic operation (+ - / *) to reach that goal. You cannot have negative numbers as result of "-" operation, and can only divide number resulting into an integer.
|
10
10
|
|
11
|
-
This gem implements a brute-force approach to solve the problem but is ready to host
|
12
|
-
Basically this strategy won't work beyond 7 numbers in the draw (it is limited in the command line
|
13
|
-
the code), as for 7 numbers
|
11
|
+
This gem implements a brute-force approach to solve the problem but is ready to host alternate strategies.
|
12
|
+
Basically this strategy won't work beyond 7 numbers in the draw (it is limited in the command line to 6 numbers but not within
|
13
|
+
the code), as for a 7 numbers draw it already takes few minutes on my machine. Although this strategy does not
|
14
14
|
stop at the first encountered solution but browses the whole tree of possibilities (that could be easily done).
|
15
15
|
|
16
16
|
To go beyond, you would probably have to go for a more mathematical approach...
|
@@ -17,7 +17,6 @@ module DigitsSolver
|
|
17
17
|
@problem_statement = problem_statement
|
18
18
|
@operands = operands
|
19
19
|
@operations_to_apply = operations_to_apply
|
20
|
-
DigitsSolver.logger.debug "New solution: #{inspect}"
|
21
20
|
end
|
22
21
|
|
23
22
|
def ==(other)
|
@@ -58,6 +57,7 @@ module DigitsSolver
|
|
58
57
|
end
|
59
58
|
|
60
59
|
def to_evaluable_code
|
60
|
+
# parenthesis management is ... meh, but better than nothing, and at least mathematically correct.
|
61
61
|
evaluable_code = operations_to_apply.each.with_index.reduce(operands.first) do |res, (operation, idx)|
|
62
62
|
op1 = res
|
63
63
|
op2 = operands[idx + 1]
|
@@ -11,7 +11,7 @@ module DigitsSolver
|
|
11
11
|
def solve_for(problem_statement, strategy: DEFAULT_STRATEGY)
|
12
12
|
extend strategy
|
13
13
|
solutions, attempts_count = solve(problem_statement)
|
14
|
-
DigitsSolver.logger.info "Found #{solutions.count} solutions amongst #{attempts_count} tries."
|
14
|
+
DigitsSolver.logger.info "Found #{solutions.count} potentially non unique solutions amongst #{attempts_count} tries."
|
15
15
|
new problem_statement, solutions, strategy
|
16
16
|
end
|
17
17
|
end
|
@@ -36,6 +36,7 @@ module DigitsSolver
|
|
36
36
|
.take(nb)
|
37
37
|
nb == 1 ? res.first : res
|
38
38
|
end
|
39
|
+
|
39
40
|
alias best_solutions best_solution
|
40
41
|
|
41
42
|
def size
|
@@ -64,9 +65,20 @@ module DigitsSolver
|
|
64
65
|
@indexed_solutions = {}
|
65
66
|
@solutions = []
|
66
67
|
|
67
|
-
solutions.
|
68
|
-
|
68
|
+
DigitsSolver.logger.info "#{solutions.count} candidate solutions"
|
69
|
+
discarded_nb = solutions.reduce(0) do |count, solution|
|
70
|
+
if self << solution
|
71
|
+
DigitsSolver.logger.debug "New solution added to solution set: #{solution.to_evaluable_code}"
|
72
|
+
count
|
73
|
+
else
|
74
|
+
count + 1
|
75
|
+
end
|
69
76
|
end
|
77
|
+
DigitsSolver.logger.info "Discarded: #{discarded_nb}"
|
78
|
+
msg = "Added #{self.solutions.count} solutions to the set of solutions, "
|
79
|
+
msg += discarded_nb.zero? ? 'none' : discarded_nb.to_s
|
80
|
+
msg += ' were discarded'
|
81
|
+
DigitsSolver.logger.info msg
|
70
82
|
end
|
71
83
|
end
|
72
84
|
end
|
@@ -25,7 +25,7 @@ module DigitsSolver
|
|
25
25
|
ops_a = OPERATIONS.to_a
|
26
26
|
number_of_possibilities = nb_operations**max_operations_number
|
27
27
|
|
28
|
-
DigitsSolver.logger.info "number of operations permutations
|
28
|
+
DigitsSolver.logger.info "The number of operations permutations for the #{nb_operations} basic operations and a draw of #{max_operations_number + 1} numbers (ie #{max_operations_number} consecutive operations) is #{number_of_possibilities}."
|
29
29
|
|
30
30
|
(0...number_of_possibilities).map do |i|
|
31
31
|
i.to_s(nb_operations)
|
@@ -43,16 +43,16 @@ module DigitsSolver
|
|
43
43
|
ops = operations_chain.take(idx + 1)
|
44
44
|
operands = ordered_draw.take(idx + 2)
|
45
45
|
yield DigitsSolver::Solution.new(problem_statement, operands, ops)
|
46
|
-
|
46
|
+
return cur_op_res
|
47
|
+
|
47
48
|
end
|
48
49
|
cur_op_res
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
53
|
def apply_operation_to_operands(operation, operand1, operand2)
|
53
|
-
|
54
|
-
|
55
|
-
res
|
54
|
+
# wow that's a useful one ;-), at least more understandable
|
55
|
+
send operation, operand1, operand2
|
56
56
|
end
|
57
57
|
|
58
58
|
def plus(a, b)
|
@@ -13,10 +13,9 @@ module DigitsSolver
|
|
13
13
|
attempts_count = 0
|
14
14
|
|
15
15
|
draw.permutation.map do |ordered_draw|
|
16
|
-
DigitsSolver.logger.debug ordered_draw.inspect
|
16
|
+
DigitsSolver.logger.debug "Testing #{ordered_draw.inspect} for all possible chains of operations"
|
17
17
|
possible_operations.each do |operations_chain|
|
18
18
|
attempts_count += 1
|
19
|
-
DigitsSolver.logger.debug operations_chain.inspect
|
20
19
|
begin
|
21
20
|
apply_operations_chain_to_ordered_draw(operations_chain, ordered_draw, problem_statement) do |valid_solution|
|
22
21
|
solutions << valid_solution
|
@@ -26,7 +25,7 @@ module DigitsSolver
|
|
26
25
|
end
|
27
26
|
end
|
28
27
|
end
|
29
|
-
DigitsSolver.logger.debug "
|
28
|
+
DigitsSolver.logger.debug "Found #{solutions.count} solutions."
|
30
29
|
[solutions, attempts_count]
|
31
30
|
end
|
32
31
|
end
|