-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_solutions.py
78 lines (66 loc) · 2.19 KB
/
compute_solutions.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
import json
import copy
class Solution:
def judgePoint24(self, cards):
sols = []
def compute(a,b,op):
if op==0:
return a+b
if op==1:
return a-b
if op==2:
return a*b
return a/b
def solve(nums):
nonlocal solCount
n = len(nums)
if n==1:
if goal-e < nums[0] < goal+e:
sols.append(copy.deepcopy(sol))
solCount += 1
return
# stop at one solution
if solCount>0:
return
for i in range(n):
for j in range(n):
if i==j: continue
for op in range(3 + (nums[j]!=0)):
if i>j and op%2==0: continue
x = compute(nums[i], nums[j], op)
nums2 = [x]
for k in range(n):
if k!=i and k!=j:
nums2.append(nums[k])
sol.append("%s = %s%s%s"%(x,nums[i],operator[op],nums[j]))
solve(nums2)
sol.pop()
e = 10**-5
goal = 24
operator = "+-*/"
sol = []
solCount = 0
solve(cards)
print(sols)
return sols
def find_str_solution(puzzle_str):
puzzle = puzzle_str.split(" ")
puzzle = [int(str_num) for str_num in puzzle]
solution = Solution()
calc = solution.judgePoint24(puzzle)
if calc:
return calc[0]
else:
return "No solution"
def add_to_json(filename):
filepath = "logs/" + filename
data = {}
with open(filepath, 'r') as f:
data = json.load(f)
for puzzle in data:
puzzle["original_puzzle"]["solution"] = find_str_solution(puzzle["original_puzzle"]["Puzzles"])
output_filepath = "logs/COT/answers_COT_game24_gpt-4o_0.7_False.json"
with open(output_filepath, 'w') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
if __name__ == "__main__":
add_to_json("COT/COT_game24_gpt-4o_0.7_False.json") # Change filename here.