-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.py
184 lines (160 loc) · 5.99 KB
/
countdown.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
179
180
181
182
183
184
import operator
import itertools
import time
from collections import defaultdict
def good_order(input_list):
"""This function determines if all 2s and 3s come before all 0s and 1s in a list"""
hit23 = False
for i in input_list:
if i > 1:
hit23 = True
if hit23 and i < 2:
return False
return True
# Global Constants, op and operator_list
op = (operator.mul, operator.div, operator.add, operator.sub)
operator_list = [] # Indices for operators, 0,1,2,3 is *,/,+,-
for i4 in xrange(4 ** 5):
cl = [i4 // 256 % 4, i4 // 64 % 4, i4 // 16 % 4, i4 // 4 % 4, i4 % 4]
if good_order(cl):
operator_list.append(cl)
print len(operator_list)
def gen_nums(loop_array):
"""Generate all possible numbers created from operations/rearrangements of a tuple"""
nums = defaultdict(set)
for line2 in loop_array:
nums_index = sorted(line2)
nums_index = tuple([int(i) for i in nums_index])
for ops in operator_list:
num = line2[0]
for i in range(1, 6):
num = op[ops[i - 1]](num, line2[i])
if 100 <= num < 1000 and num.is_integer():
nums[nums_index].add(int(num))
return nums
def write_file(nums, option_type):
with open('results/'+option_type+'.txt', 'w') as f:
for num in nums:
f.write(str(num)+';')
for i in sorted(nums[num]):
f.write(str(i)+';')
f.write("\n")
print len(nums)
def main():
L_array = [25., 50., 75., 100.]
s_array = [float(i) for i in range(1, 11)]
start = time.time()
option = 9
all_options = False
# Labcde
if option == 0 or all_options:
option_type = "Labcde"
print option_type, option
nums = {}
for L in L_array:
for s in itertools.combinations(s_array, 5):
loop_array = itertools.permutations([L] + list(s))
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# Laabcd
if option == 1 or all_options:
option_type = "Laabcd"
print option_type, option
nums = {}
for s in itertools.combinations(s_array, 4):
for L in L_array:
for s0 in s:
loop_array = itertools.permutations([L] + list(s) + [s0])
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# Laabbc
if option == 2 or all_options:
option_type = "Laabbc"
print option_type, option
nums = {}
for s in itertools.permutations(s_array, 3):
for L in L_array:
loop_array = itertools.permutations([L] + list(s) + [s[0], s[1]])
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# LLabcd
if option == 3 or all_options:
option_type = "LLabcd"
print option_type, option
nums = {}
for s in itertools.combinations(s_array, 4):
for L in itertools.combinations(L_array, 2):
loop_array = itertools.permutations(list(L) + list(s))
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# LLaabc
if option == 4 or all_options:
option_type = "LLaabc"
print option_type, option
nums = {}
for s in itertools.combinations(s_array, 3):
for L in itertools.combinations(L_array, 2):
for s0 in s:
loop_array = itertools.permutations(list(L) + list(s) + [s0])
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# LLaabb
if option == 5 or all_options:
option_type = "LLaabb"
print option_type, option
nums = {}
for s in itertools.combinations(s_array, 2):
for L in itertools.combinations(L_array, 2):
loop_array = itertools.permutations(list(L) + list(s) + list(s))
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# LLLabc
if option == 6 or all_options:
option_type = "LLLabc"
print option_type, option
nums = {}
for s in itertools.combinations(s_array, 3):
for L in itertools.combinations(L_array, 3):
loop_array = itertools.permutations(list(L) + list(s))
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# LLLaab
if option == 7 or all_options:
option_type = "LLLaab"
print option_type, option
nums = {}
for s in itertools.combinations(s_array, 2):
for L in itertools.combinations(L_array, 3):
for s0 in s:
loop_array = itertools.permutations(list(L) + list(s) + [s0])
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time() - start, " time elapsed\n"
# LLLLab
if option == 8 or all_options:
option_type = "LLLLab"
print option_type, option
nums = {}
for s in itertools.permutations(s_array, 2):
loop_array = itertools.permutations(L_array + list(s))
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time()-start, " time elapsed\n"
# LLLLaa
if option == 9 or all_options:
option_type = "LLLLaa"
nums = {}
for a in s_array:
loop_array = itertools.permutations(L_array+[a, a])
nums.update(gen_nums(loop_array))
write_file(nums, option_type)
print time.time()-start, " time elapsed\n"
if __name__ == '__main__':
main()