-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
146 lines (115 loc) · 2.89 KB
/
generate.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
class Operator() :
pass
class Add(Operator) :
def __repr__(self):
return "'+'"
def __call__(self, x, y):
return x+y
@property
def first(self):
return set(range(1,999))
def second_for(self, x):
return set(range(1, 1000-x))
class Sub(Operator) :
def __repr__(self):
return "'-'"
def __call__(self, x, y):
return x-y
@property
def first(self):
return set(range(2,1000))
def second_for(self, x):
return set(range(1, x))
class Mul(Operator) :
def __repr__(self):
return "'*'"
def __call__(self, x, y):
return x*y
@property
def first(self):
return set(range(2,500))
def second_for(self, x):
return set(range(2,(999/x)+1))
class Div(Operator) :
def __repr__(self):
return "'/'"
def __call__(self, x, y):
return float(x)/y
@property
def first(self):
return set(range(2,1000))
def second_for(self, x):
yset = set(divisors(x))
yset.discard(1)
yset.discard(x)
return yset
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return set(factors)
def divisors(n):
import math
large_divisors = []
for i in xrange(1, int(math.sqrt(n) + 1)):
if n % i == 0:
yield i
if i*i != n:
large_divisors.append(n / i)
for divisor in reversed(large_divisors):
yield divisor
def valid_puzzle(ops, nums):
x = ops[0](nums[0], nums[1])
y = ops[1](nums[2], nums[3])
u = ops[3](nums[0], nums[2])
v = ops[4](nums[1], nums[3])
z = ops[2](u, v)
return z == ops[5](x, y) and all(0<i<1000 for i in nums+(u,v,x,y,z))
def all_unique(ops, nums):
x = ops[0](nums[0], nums[1])
y = ops[1](nums[2], nums[3])
u = ops[3](nums[0], nums[2])
v = ops[4](nums[1], nums[3])
z = ops[2](u, v)
return len(set(nums+(x,y,u,v,z))) == 9
def operator_assignments():
ops = [Div(), Mul(), Sub(), Add()]
for op1 in ops :
for op2 in ops :
for op3 in ops :
for op4 in ops :
for op5 in ops :
for op6 in ops :
yield op1, op2, op3, op4, op5, op6
import sys
# n0 o0 n1 == xx
# o3 o4 o5
# n2 o1 n3 == yy
# == == ==
# uu o2 vv == zz
operators = list(operator_assignments())
#import random
#random.shuffle(operators)
for op in operators :
if len(set(op[:3])) == 1 or len(set(op[:3])) == 1: continue
if not 0 < len([o for o in op if isinstance(o, (Mul, Div))]) <= 3 : continue
for n0 in op[0].first & op[3].first:
if not op[0].second_for(n0) or not op[3].second_for(n0): continue
for n1 in op[0].second_for(n0) & op[4].first:
if not op[4].second_for(n1): continue
for n2 in op[1].first & op[3].second_for(n0):
for n3 in op[1].second_for(n2) & op[4].second_for(n1):
nums = (n0,n1,n2,n3)
sys.stderr.write("\rtrying: %r %r " % (op, nums))
if not all_unique(op, nums): continue
if valid_puzzle(op, nums):
sys.stdout.write("\t\t[%r, %r, %r, %r, %r, %r, %s, %s, %s, %s],\n" % (op+nums))
sys.stdout.flush()
sys.stderr.write("\n")