-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path93.py
More file actions
44 lines (38 loc) · 1.13 KB
/
Copy path93.py
File metadata and controls
44 lines (38 loc) · 1.13 KB
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
from itertools import combinations, product,permutations
from operator import add, sub, mul, truediv as div
ops = (add,sub,mul,div)
def get_targets(digits,ops,lhs=None):
if len(digits) == 0:
return [lhs]
rhs = digits[0]
digits = digits[1:]
if lhs==None:
return get_targets(digits,ops,rhs)
op = ops[0]
ops = ops[1:]
out = []
for orhs in get_targets(digits,ops,rhs):
try:
out.append(op(lhs,orhs))
except ZeroDivisionError:
pass
if len(ops) > 0:
try:
out.extend(get_targets(digits,ops,op(lhs,rhs)))
except ZeroDivisionError:
pass
return out
best = 0
best_digits = None
for digits in combinations(range(1,10),4):
targets = set()
for digorder in permutations(digits,4):
for oporder in product(ops,repeat=3):
targets.update(filter(lambda x: x % 1 == 0 and x > 0, get_targets(digorder,oporder)))
expressible = 0
while expressible+1 in targets:
expressible+=1
if expressible > best:
best = expressible
best_digits = digits
print(best, best_digits)