65 lines
1.2 KiB
Python
65 lines
1.2 KiB
Python
|
#
|
||
|
def choose(xs, n):
|
||
|
"""
|
||
|
Computes r choose n, in other words choose n from xs.
|
||
|
Returns combinations and not permutations.
|
||
|
"""
|
||
|
|
||
|
if not xs or n == 0:
|
||
|
return [[]]
|
||
|
if len(xs) == n:
|
||
|
return [xs]
|
||
|
|
||
|
rs = []
|
||
|
x = xs[0]
|
||
|
for r in choose(xs[1:], n - 1):
|
||
|
rs.append([x] + r)
|
||
|
for r in choose(xs[1:], n):
|
||
|
rs.append(r)
|
||
|
return rs
|
||
|
|
||
|
|
||
|
def pairs(xs):
|
||
|
if len(xs) == 2:
|
||
|
return [xs]
|
||
|
r = []
|
||
|
a = xs[0]
|
||
|
for b in xs[1:]:
|
||
|
r.append([a, b])
|
||
|
|
||
|
return r + pairs(xs[1:])
|
||
|
|
||
|
|
||
|
def can_form_squares(pair):
|
||
|
xs, ys = pair
|
||
|
|
||
|
# replace 9s with 6s
|
||
|
for d, e in [(0, 1), (0, 4), (0, 6), (1, 6),
|
||
|
(2, 5), (3, 6), (4, 6), (6, 4), (8, 1)]:
|
||
|
if d in xs and e in ys:
|
||
|
continue
|
||
|
elif e in xs and d in ys:
|
||
|
continue
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
|
||
|
def euler_090():
|
||
|
# replace 9s with 6s to handle "up-side down usage of 6" without hassle
|
||
|
xs = list(range(0, 9)) + [6]
|
||
|
|
||
|
cs = choose(xs, 6)
|
||
|
ps = pairs(cs)
|
||
|
count = 0
|
||
|
for p in ps:
|
||
|
if can_form_squares(p):
|
||
|
count += 1
|
||
|
return count
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
solution = euler_090()
|
||
|
print("e090.py: " + str(solution))
|
||
|
assert(solution == 1217)
|
||
|
|