Class: Calc24::TwentyFourGamePlayer
- Inherits:
-
Object
- Object
- Calc24::TwentyFourGamePlayer
- Defined in:
- lib/calc24/calc24_core.rb
Constant Summary collapse
- MAN4D4S =
[ # '((%d %s %d) %s %d) %s %d' 48 # +++ '%d + %d + %d + %d', # ++- '%d + %d + %d - %d', # ++* '(%d + %d + %d) * %d', '(%d + %d) * %d + %d', '%d * %d + %d + %d', # ++/ '(%d + %d + %d) / %d', '(%d + %d) / %d + %d', '(%d / %d) + %d + %d', # +-- '%d + %d - %d - %d', # +-* '(%d + %d - %d) * %d', '(%d + %d) * %d - %d', '(%d - %d) * %d + %d', '%d * %d + %d - %d', # +-/ '(%d + %d - %d) / %d', '(%d + %d) / %d - %d', '(%d - %d) / %d + %d', '%d / %d + %d - %d', # +** '(%d + %d) * %d * %d', '(%d * %d + %d) * %d', '%d * %d * %d + %d', # +*/ '(%d + %d) * %d / %d', '(%d * %d + %d) / %d', '%d * %d / %d + %d', '(%d / %d + %d) * %d', # +// '(%d + %d) / %d / %d', '(%d / %d + %d) / %d', '%d / %d / %d + %d', # --- '%d - %d - %d - %d', # --* '(%d - %d - %d) * %d', '(%d - %d) * %d - %d', '%d * %d - %d - %d', # --/ '(%d - %d - %d) / %d', '(%d - %d) / %d - %d', '%d / %d - %d - %d', # -** '(%d - %d) * %d * %d', '(%d * %d - %d) * %d', '%d * %d * %d - %d', # -*/ '(%d - %d) * %d / %d', '(%d * %d - %d) / %d', '%d * %d / %d - %d', '(%d / %d - %d) * %d', # -// '(%d - %d) / %d / %d', '(%d / %d - %d) / %d', '%d / %d / %d - %d', # *** '%d * %d * %d * %d', # **/ '%d * %d * %d / %d', # *// '%d * %d / %d / %d', # /// '%d / %d / %d / %d', # '(%d %s (%d %s %d)) %s %d' +8 '(%d - %d * %d) * %d', '(%d - %d / %d) * %d', '%d / (%d + %d) * %d', '%d / (%d - %d) * %d', '(%d - %d * %d) / %d', '(%d - %d / %d) / %d', '%d / (%d + %d) / %d', '%d / (%d - %d) / %d', # '(%d %s %d) %s (%d %s %d)' +14 '%d * %d + %d * %d', '%d * %d + %d / %d', '%d / %d + %d / %d', '%d * %d - %d * %d', '%d * %d - %d / %d', '%d / %d - %d * %d', '%d / %d - %d / %d', '(%d + %d) * (%d + %d)', '(%d + %d) * (%d - %d)', '(%d - %d) * (%d - %d)', '(%d + %d) / (%d + %d)', '(%d + %d) / (%d - %d)', '(%d - %d) / (%d + %d)', '(%d - %d) / (%d - %d)', # '%d %s ((%d %s %d) %s %d)' +14 '%d - ((%d + %d) * %d)', '%d - ((%d + %d) / %d)', '%d - ((%d - %d) * %d)', '%d - ((%d - %d) / %d)', '%d - (%d * %d * %d)', '%d - (%d * %d / %d)', '%d - (%d / %d / %d)', '%d / (%d + %d + %d)', '%d / (%d + %d - %d)', '%d / (%d - %d - %d)', '%d / (%d * %d + %d)', '%d / (%d * %d - %d)', '%d / (%d / %d + %d)', '%d / (%d / %d - %d)', # '%d %s (%d %s (%d %s %d))' +4 '%d - (%d / (%d + %d))', '%d - (%d / (%d - %d))', '%d / (%d - %d * %d)', '%d / (%d - %d / %d)' ]
- @@objective =
Rational(24,1)
Instance Attribute Summary collapse
-
#numbers ⇒ Object
readonly
Returns the value of attribute numbers.
-
#solutions ⇒ Object
readonly
Returns the value of attribute solutions.
Instance Method Summary collapse
-
#initialize(numbers) ⇒ TwentyFourGamePlayer
constructor
A new instance of TwentyFourGamePlayer.
- #solve ⇒ Object
Constructor Details
#initialize(numbers) ⇒ TwentyFourGamePlayer
Returns a new instance of TwentyFourGamePlayer.
128 129 130 131 132 |
# File 'lib/calc24/calc24_core.rb', line 128 def initialize(numbers) @numbers = numbers @solutions = {} solve end |
Instance Attribute Details
#numbers ⇒ Object (readonly)
Returns the value of attribute numbers.
134 135 136 |
# File 'lib/calc24/calc24_core.rb', line 134 def numbers @numbers end |
#solutions ⇒ Object (readonly)
Returns the value of attribute solutions.
134 135 136 |
# File 'lib/calc24/calc24_core.rb', line 134 def solutions @solutions end |
Instance Method Details
#solve ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/calc24/calc24_core.rb', line 136 def solve numbers.permutation.to_a.uniq.each do |a,b,c,d| MAN4D4S.each do |expr| # evaluate using rational arithmetic test = expr.gsub('%d', 'Rational(%d,1)') % [a, b, c, d] value = eval(test) rescue -1 # catch division by zero if value == @@objective @solutions[expr] = expr % [a, b, c, d] end end end end |