-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.py
executable file
·178 lines (161 loc) · 7.57 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/python
import random
import parser
import re
import math
import itertools
from combinatorial import Combinatorial
import operation
class Solver:
def __init__(self):
self.cards = list(range(2, 11)) + ['J', 'Q', 'K', 'A']
self.card_re = re.compile(r'[1-9JQKA]0?')
self.hand = []
# choose the operations you're interested in supporting...
self.ops = [operation.Add, operation.Subtract, operation.Multiply,
operation.Divide, operation.Exponent, operation.Log]
def get_next_hand(self):
self.hand = [random.choice(self.cards) for x in range(4)]
return self.hand
def set_hand(self, card_str):
cards = self.card_re.findall(card_str)
# must input 4 cards
if len(cards) != 4:
print("ERROR: please enter 4 cards")
return None
self.hand = cards
def get_solutions(self):
hands = []
solutions = []
hand = [x if x is not "A" else 1 for x in self.hand] # replace all Aces with 1
facecards = [x for x in hand if not str(x).isdigit()]
for i in range(2**len(facecards)):
new_hand = []
face = 1
for card in hand:
if not str(card).isdigit():
if card is "J":
new_hand.append(11 if (i // face) % 2 == 0 else 10)
elif card is "Q":
new_hand.append(12 if (i // face) % 2 == 0 else 10)
else: # K
new_hand.append(13 if (i // face) % 2 == 0 else 10)
face *= 2
else:
new_hand.append(int(card))
hands.append(new_hand)
for hand in hands:
potential_solutions = list(itertools.permutations(hand))
for solution in potential_solutions: # for all possible card combinations...
c = Combinatorial(self.ops, 3)
while c.next(): # for all possible operation combinations...
opers = c.get_items()
try:
# (a b) (c d)
left = opers[0].operate(solution[0], solution[1])
right = opers[1].operate(solution[2], solution[3])
answer = opers[2].operate(left, right)
if answer == 24:
solutions.append((opers, 0, solution))
except Exception as e:
pass # it's definitely not a valid solution if there's overflow, divide by zero, etc
try:
# ((a b) c) d
left = opers[0].operate(solution[0], solution[1])
right = opers[1].operate(left, solution[2])
answer = opers[2].operate(right, solution[3])
if answer == 24:
solutions.append((opers, 1, solution))
except Exception as e:
pass # it's definitely not a valid solution if there's overflow, divide by zero, etc
try:
# (a (b c)) d
left = opers[0].operate(solution[1], solution[2])
right = opers[1].operate(solution[0], left)
answer = opers[2].operate(right, solution[3])
if answer == 24:
solutions.append((opers, 2, solution))
except Exception as e:
pass # it's definitely not a valid solution if there's overflow, divide by zero, etc
try:
# a ((b c) d)
left = opers[0].operate(solution[1], solution[2])
right = opers[1].operate(left, solution[3])
answer = opers[2].operate(solution[0], right)
if answer == 24:
solutions.append((opers, 3, solution))
except Exception as e:
pass # it's definitely not a valid solution if there's overflow, divide by zero, etc
try:
# a (b (c d))
left = opers[0].operate(solution[2], solution[3])
right = opers[1].operate(solution[1], left)
answer = opers[2].operate(solution[0], right)
if answer == 24:
solutions.append((opers, 4, solution))
except Exception as e:
pass # it's definitely not a valid solution if there's overflow, divide by zero, etc
return None if len(solutions) == 0 else solutions
def check_solution(self, solution):
cards = self.card_re.findall(solution)
# must input 4 numbers
if len(cards) != 4:
print("ERROR: must use all 4 numbers")
return False
card_copy = [str(c) for c in self.hand]
for card in [str(c) for c in cards]:
if card in card_copy:
card_copy.remove(card)
else:
# used card which doesn't match
print("ERROR: invalid use of card", card)
return False
hands = []
solution = solution.replace('A', "1")
facecards = [c for c in [str(c) for c in cards] if not c.isdigit()]
for i in range(2**len(facecards)):
new_hand = solution
face = 1
for card in facecards:
if card is "J":
new_hand = new_hand.replace("J", "11" if (i // face) % 2 == 0 else "10")
elif card is "Q":
new_hand = new_hand.replace("Q", "12" if (i // face) % 2 == 0 else "10")
else: # K
new_hand = new_hand.replace("K", "13" if (i // face) % 2 == 0 else "10")
face *= 2
hands.append(new_hand)
for hand in hands:
try:
code = parser.expr(hand).compile()
if eval(code) == 24:
return True
except Exception as e:
print(e)
return False
def print_solutions(solutions):
for solution in solutions:
oper = solution[0]
parens = solution[1]
cards = solution[2]
if parens == 0: # (a b) (c d)
left = "(" + oper[0].to_string(cards[0], cards[1]) + ")"
right = "(" + oper[1].to_string(cards[2], cards[3]) + ")"
output = oper[2].to_string(left, right)
elif parens == 1: # ((a b) c) d
left = "(" + oper[0].to_string(cards[0], cards[1]) + ")"
right = "(" + oper[1].to_string(left, cards[2]) + ")"
output = oper[2].to_string(right, cards[3])
elif parens == 2: # (a (b c)) d
left = "(" + oper[0].to_string(cards[1], cards[2]) + ")"
right = "(" + oper[1].to_string(cards[0], left) + ")"
output = oper[2].to_string(right, cards[3])
elif parens == 3: # a ((b c) d)
left = "(" + oper[0].to_string(cards[1], cards[2]) + ")"
right = "(" + oper[1].to_string(left, cards[3]) + ")"
output = oper[2].to_string(cards[0], right)
else: # a (b (c d))
left = "(" + oper[0].to_string(cards[2], cards[3]) + ")"
right = "(" + oper[1].to_string(cards[1], left) + ")"
output = oper[2].to_string(cards[0], right)
print(output)