私がオタクのグループと一緒に遊んでいるような楽しいカードゲームがあります。Pythonカードゲーム
ルールは単純です:1人が4枚のカードを置く。このゲームのバージョンでは、ロイヤルティは10とカウントされます。ゲームのポイントは、任意のバイナリ演算子を使用して24を作ることです。例として:
10、2、3、6
(10-6)*(2 * 3)
4 * 6
そう...なぜ私はブルートフォースでこれを解決するためにコンピュータを使用しないでください?
from math import floor,ceil
for i in range(1000): # not 10,000 yet. It would run too long.
one, two, three, four = i//(1000)+1,(i%1000)//100+1,(i%100)//10+1,i%10+1
operand = [int.__add__,
int.__sub__,
int.__mul__,
int.__truediv__,
int.__pow__,
int.__mod__,
int.__or__, # even though I'd have to be a pedant to use these, I'm still curious
int.__and__,
int.__xor__,
int.__lshift__,
int.__rshift__]
op_str = {int.__add__:"+",
int.__sub__:"-",
int.__mul__:"*",
int.__truediv__:"/",
int.__pow__:"**",
int.__mod__:"%",
int.__or__:"|",
int.__and__:"&",
int.__xor__:"^",
int.__lshift__:"<<",
int.__rshift__:">>"}
for j in range(pow(len(operand),3)):
try:
a = operand[j%len(operand)]
b = operand[(j%(len(operand)**2))//len(operand)]
c = operand[(j%(len(operand)**3))//(len(operand)**2)]
# serial
answer = a(one,b(two,c(three,four)))
if ceil(answer) == floor(answer) and answer == 24:
print(one, op_str[a], "(", two, op_str[b], "(", three, op_str[c], four, "))")
print(one, op_str[a], "(", two, op_str[b], "(", c(three, four), "))")
print(one, op_str[a], "(", b(two,c(three,four)), ")")
print(a(one,b(two,c(three,four))))
continue
# tree
answer = c(a(one, two), b(three, four))
if ceil(answer) == floor(answer) and answer == 24:
print("((", one, op_str[a], two, ")", op_str[b], "(", three, op_str[c], four, "))")
print("(", a(one, two), op_str[b], c(three, four), ")")
print(c(a(one, two), b(three, four)))
continue
except Exception:
pass # I just want to bypass any stupid problems with modulus, divide and the shifts
を除き、私は愚かな答えを得ている:問題を見て
...
((1 % 1) * (6 | 4))
(0 * 6)
24
((1 - 1) * (6 + 4))
(0 * 10)
24
...
誰?